首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android EditText 輸入完成後隱藏光標:轉移焦點或設置 setEnabled(false)

     編輯:程式設計綜合問答

用戶點擊「完成」鍵後,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

editText.setOnEditorActionListener((v, actionId, event) -> {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        editText.setEnabled(false); // 光標消失
        editText.setEnabled(true);  // 立即恢復可編輯(可選)
        return true;
    }
    return false;
});

XML 屬性輔助

<EditText
    android:imeOptions="actionDone"
    android:focusable="true"
    android:focusableInTouchMode="true" />

焦點轉移最自然,禁用 EditText 需謹慎處理狀態恢復。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved