开发者

Force show soft keyboard from within an AlertDialog

I have the following code which I think should work to force the keyboard to be shown when the Alert Dialog is shown.

public void showTypeBox(){
        edit = new EditText(this);
        edit.setPadding(10, 0, 0, 10);

        AlertDialog dia开发者_Python百科log = new AlertDialog.Builder(this)
            .setTitle("Type word to search for:")
            .setPositiveButton("Search", Main.this)
            .setNegativeButton("Cancel", null)
            .setView(edit)
            .setIcon(R.drawable.menu_icon)
            .create();
        dialog.show();

        InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
    }

I can't see what i'm doing wrong here.


You're trying to show the keyboard before the EditText is laid out and visible. Try this:

Handler delayedRun = new Handler();
delayedRun.post(new Runnable() {
  @Override
  public void run() {
    edit.requestFocus();
    InputMethodManager mgr = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(addressBox, InputMethodManager.SHOW_IMPLICIT);
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜