點擊事件觸發對話框顯示的同時,自動彈出軟鍵盤,讓用戶直接輸入文字。
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
// 在點擊監聽器或對話框顯示後調用
Button btn = findViewById(R.id.btn_show_dialog);
btn.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);
AlertDialog dialog = builder.create();
dialog.show();
// 關鍵:對話框顯示後,請求焦點並顯示軟鍵盤
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
});