我一直在努力尋找適用于我的特定用例的解決方案,但我還沒有找到合適的設置。我有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); }
添加回答
舉報
0/150
提交
取消