开发者

Force show soft keyboard from within an AlertDialog

开发者 https://www.devze.com 2023-03-12 05:43 出处:网络
I have the following code which I think should work to force the keyboard to be shown when the Alert Dialog is shown.

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

精彩评论

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

关注公众号