对话框关闭时隐藏软输入键盘

gat*_*rob 9 android android-softkeyboard android-dialog

我正在打开Dialog一个Activity.对话框打开后,我打电话

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)

问题是,当我通过点击取消按钮或单击对话框外部关闭对话框时,键盘切换到文本键盘并且不会消失工具我单击硬件返回按钮.如何在关闭对话框时关闭键盘,并将焦点返回到上一个窗口?

小智 9

AndroidManifest.xml中,在Activity中设置显示Dialog的属性

机器人:windowSoftInputMode = "stateAlwaysHidden"

注意!不是州长,是州总是隐藏的.它会在Dismiss of Dialog上自动隐藏软键盘.

希望拯救你的生命.


Ven*_*h S 1

AlertDialog.Builder builder = new AlertDialog.Builder(EllipticalActivity.this);
builder.setTitle("title")
       .setMessage("message")
       .setCancelable(false)
       .setNegativeButton("Close", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               InputMethodManager inputManager = 
                   (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                                    InputMethodManager.HIDE_NOT_ALWAYS);
               dialog.cancel();
           }
        });
        AlertDialog alert = builder.create();
        alert.show();
Run Code Online (Sandbox Code Playgroud)