用戶點擊「完成」鍵後,EditText 仍保持焦點顯示閃爍光標。解決:將焦點移至其他 View,或暫時禁用 EditText。
editText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// 找一個可聚焦的 View(如按鈕、父佈局)
findViewById(R.id.someOtherView).requestFocus();
// 可選:隱藏鍵盤
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
});
editText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
editText.setEnabled(false); // 光標消失
editText.setEnabled(true); // 立即恢復可編輯(可選)
return true;
}
return false;
});
<EditText
android:imeOptions="actionDone"
android:focusable="true"
android:focusableInTouchMode="true" />
焦點轉移最自然,禁用 EditText 需謹慎處理狀態恢復。