我有一个输入数字的对话框。当我从活动中调用它然后将其关闭时,软键盘会隐藏起来。但是当我从另一个对话框中打开它时出现问题。在这种情况下,当我在对话框外部单击时,对话框将关闭,但键盘仍然可见,即使显式调用hideSoftKeyboard
也不能解决问题。但是,当我使用否定或肯定按钮的键盘隐藏对话框时。
这是我的代码:
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);
}
}
答案 0 :(得分:1)
尝试一下:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);