解決:有EditText的自定義AlertDialog顯示時軟鍵盤不彈出

  • ※ 以下這段代碼要放在 dialog.show(); 或 builder.create().show(); 方法下面。

  • ※ 以下這段代碼要放在 dialog.show(); 或 builder.create().show(); 方法下面。

  • ※ 以下這段代碼要放在 dialog.show(); 或 builder.create().show(); 方法下面。

    
            new Handler().postDelayed(() -> {
                // EditText設置可獲得焦點
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(editText, 0);
            }, 1);

    設置軟鍵盤延遲1毫秒彈起,彈起軟鍵盤的時間和AlertDialog的彈出時間幾乎是同時,測試過沒有問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章