EditText光標不顯示問題

前言

之前項目中碰到過,好久不用又忘了,今天在這裏做個筆記方便日後查詢吧。

內容

先看下我的EditText標籤

<EditText
        android:id="@+id/et_org"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@android:color/white"
        android:hint="請輸入內容"
        android:maxLength="20"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textSize="16sp"
        >
    </EditText>

也沒有添加比較特殊的屬性,但光標就是無緣無故的消失了,在這裏猜測可能是安卓的一個bug吧,有哪位大神知道可以留言指點
這裏直接說下解決辦法吧,此處可以直接通過添加

android:textCursorDrawable="@null"

加上這句光標就出來了,不過在有的機型上可能會變成空心,也就是隻有兩條豎線的空心。
這樣的話我們可以通過自定義一個shape來達到理想效果
1、在資源文件drawable下新建一個光標控制cursor_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="#000000"  />
</shape>

2、設置EditText:EditText:android:textCursorDrawable=”@drawable/cursor_color”
這樣不僅顯示正常,還可以改變光標顏色!

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