I have an item in my application, click on it and it pops up an alert dialog with a text box and a button. It's a bit clunky, the text box isn't focused so you have to click it to bring up the keyboard. Is there a better way to prompt for text input?
Asked
Active
Viewed 975 times
3 Answers
1
If you need to let it always open you can use
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
And then use editText.requestFocus to get the text into there.
Or use the android:inputMethod on the EditText layout and add the "alwaysVisible" flag.

Marcos Vasconcelos
- 18,136
- 30
- 106
- 167
0
You can create a focus listener on the EditText on the AlertDialog, then get the AlertDialog's Window. From there you can make the soft keyboard show by calling setSoftInputMode.
final AlertDialog dialog = ...;
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});

evilone
- 22,410
- 7
- 80
- 107
0
AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this);
builder.setTitle(params[4]);
final EditText et = new EditText(tab3.this);
et.setText(params[5]);
builder.setView(et);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
params[5] = et.getText().toString();
ViewGroup vg = (ViewGroup) arg1;
TextView txv = (TextView)vg.findViewById(R.id.datetext_view2);
txv.setText(params[5]);
form_adapter.datas.put(ckey, params);
save(GlobalVars.current_id,form_adapter.datas);
}
});
final AlertDialog alert = builder.create();
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
alert.show();

2red13
- 11,197
- 8
- 40
- 52