为了账号安全,请及时绑定邮箱和手机立即绑定

如何将软键盘隐藏在自定义警报对话框中?

如何将软键盘隐藏在自定义警报对话框中?

至尊宝的传说 2023-03-02 10:24:55
我一直在努力寻找适用于我的特定用例的解决方案,但我还没有找到合适的设置。我有AlertDialog一个习俗EditText。我在没有系统键盘的情况下处理文本输入,所以当我在屏幕上显示对话框时弹出它是一个问题。我已经研究了许多不同的解决方案,但没有一个对我有用。这个Stack Overflow 问题以及这个问题、这个Medium 文章和这个教程。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);}如果有人能确定我错过了什么小细节(或大的、明显的代码块)来阻止键盘显示,我将不胜感激。
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

经过更多的挖掘,我找到了我的用例所需的解决方案。键盘不出现,但光标停留。

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);
}


查看完整回答
反对 回复 2023-03-02
  • 1 回答
  • 0 关注
  • 53 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信