EditText 初始 enabled="false",程式碼動態啟用並請求焦點、彈出鍵盤。輸入文字後全選刪除,光標隨即消失,無法繼續輸入。
android:inputType="none" 導致編輯器不顯示光標(無輸入法連接)。setInputType(TYPE_CLASS_TEXT) 雖改變輸入類型,但若 View 已附著於視窗,可能需 setText() 觸發重繪。android:textCursorDrawable 控制,預設可能與背景同色(白底白光標)。<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:inputType="text" <!-- 移除 "none" -->
android:text="@string/nicheng2"
android:textColor="@color/gray5"
android:textSize="@dimen/textSize_1"
android:enabled="false"
android:textCursorDrawable="@null" /> <!-- 光標跟隨文字顏色 -->
程式碼中僅需 edit.setEnabled(true),光標自動顯示。
edit.setEnabled(true);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
// 強制刷新光標可見性
edit.setText(edit.getText()); // 觸發內部 Editor 重建
edit.setSelection(edit.length());
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
在 styles.xml 定義:
<style name="Widget.EditText.Cursor" parent="Widget.AppCompat.EditText">
<item name="android:textCursorDrawable">@null</item>
<item name="colorControlActivated">@color/your_accent_color</item>
</style>
佈局引用 style="@style/Widget.EditText.Cursor"。
textCursorDrawable="@null" 完美生效。TextView.mCursorDrawableRes 欄位,或降低 targetSdkVersion 至 10(不推薦,影響其他控件樣式)。inputType="text" 而非 "none"。android:textCursorDrawable="@null" 確保光標顏色跟隨文字。setText(getText()) 刷新編輯器狀態。OnFocusChangeListener 中複雜操作導致焦點震盪。