如何将软键盘隐藏在自定义警报对话框中?
How to keep the softkeyboard hidden in a custom alertdialog?
我一直在努力寻找适用于我的特定用例的解决方案,但我还没有找到合适的设置。我有一个带有 EditText
的自定义 AlertDialog
。我在没有系统键盘的情况下处理文本输入,所以当我在屏幕上显示对话框时弹出它是一个问题。
我已经研究了许多不同的解决方案,但其中 none 对我有用。这个Stack Overflow question as well as this one, this Medium article, and this tutorial.
public void showAlert() {
alert.show();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
// using this view to hide the keyboard causes a NPE
View view = ((AppCompatActivity) context).getCurrentFocus();
// I have also passed in a 0 for this one.
imm.hideSoftInputFromWindow(ttsAlert.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
// This is the editText in the view
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// This is the rootView of the custom layout
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
editText.setShowSoftInputOnFocus(false);
}
如果有人能确定我错过了什么小细节(或大的、明显的代码块)来阻止键盘显示,我将不胜感激。
经过更多的挖掘,我找到了我的用例所需的解决方案。键盘不出现,光标停留
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
现在的方法很简单:
public void showAlert() {
alert.show();
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
我一直在努力寻找适用于我的特定用例的解决方案,但我还没有找到合适的设置。我有一个带有 EditText
的自定义 AlertDialog
。我在没有系统键盘的情况下处理文本输入,所以当我在屏幕上显示对话框时弹出它是一个问题。
我已经研究了许多不同的解决方案,但其中 none 对我有用。这个Stack Overflow question as well as this one, this Medium article, and this tutorial.
public void showAlert() {
alert.show();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
// using this view to hide the keyboard causes a NPE
View view = ((AppCompatActivity) context).getCurrentFocus();
// I have also passed in a 0 for this one.
imm.hideSoftInputFromWindow(ttsAlert.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
// This is the editText in the view
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// This is the rootView of the custom layout
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
editText.setShowSoftInputOnFocus(false);
}
如果有人能确定我错过了什么小细节(或大的、明显的代码块)来阻止键盘显示,我将不胜感激。
经过更多的挖掘,我找到了我的用例所需的解决方案。键盘不出现,光标停留
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
现在的方法很简单:
public void showAlert() {
alert.show();
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}