我一直在尝试寻找适合我的特定用例的解决方案,但我尚未找到正确的设置。我有一个带有 EditText
的自定义 AlertDialog
。我在没有系统键盘的情况下处理文本输入,因此当我在屏幕上显示对话框时弹出它是一个问题。
我已经研究过许多不同的解决方案,但没有一个对我有用。这个Stack Overflow question以及this one ,这个Medium article ,和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);
}
关于java - 如何在自定义警报对话框中隐藏软键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837358/