问题
So i have a menu item that shows AlertDialog
with a EditText
in it, the problem is that although it is focused the softkeyboard doesn show until I click on the edittext, anyone got a solution ? I tried
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
but it doesn work. Here is my code
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return (applyMenuChoice(item) || super.onOptionsItemSelected(item));
}
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId()) {
case SEARCH:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setMinimumWidth(300);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
});
alert.show();
return (true);
case DELETE:
getListView().setAdapter(null);
return (true);
}
return (false);
}
回答1:
Try this code,
TO OPEN
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);
回答2:
The below solution works for me
Just comment the
alert.show();
in your code and embed the below code
AlertDialog alertDlg = alert.create();
alertDlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDlg.show();
Hard-coded delays are never recommended because they may introduce unpredictable behavior under different conditions / different devices.
来源:https://stackoverflow.com/questions/7378043/softkeyboard-not-showing-in-a-alertdialog