简体   繁体   English

当从另一个对话框中调用对话框时,软键盘不会在对话框关闭时隐藏

[英]Soft keyboard doesn't hide on dialog dismiss when the dialog is called from another one

I have a dialog for number input. 我有一个用于输入数字的对话框。 When I call it from an activity and then dismiss it, soft keyboard hides. 当我从活动中调用它然后将其关闭时,软键盘会隐藏起来。 But there is a problem when I open it from another dialog. 但是当我从另一个对话框中打开它时出现问题。 In this case, when I click outside of the dialog, my dialog closes but the keyboard is still visible, even calling hideSoftKeyboard explicitly doesn't solve the problem. 在这种情况下,当我在对话框外部单击时,对话框将关闭,但键盘仍然可见,即使显式调用hideSoftKeyboard也不能解决问题。 But when I close the dialog using negative or positive button keyboard hides. 但是,当我使用否定或肯定按钮关闭对话框时,会隐藏键盘。

Here is my code: 这是我的代码:

builder.setPositiveButton(R.string.ok_button, (dialog, which) -> {
    ...
    hideSoftKeyboard(editText, requireActivity());
});

builder.setNegativeButton(R.string.cancel_button, (dialog, which) -> hideSoftKeyboard(editText, requireActivity()));


public static void hideSoftKeyboard(View view, Context context) {
    if (view != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

尝试这个:

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM