前幾天項目中發生了一件靈異事件。我的一個登錄頁面Edittext居然沒有光標閃爍了。
我以為是誰搶了焦點,于是在代碼中動態獲取焦點,發現仍然沒有。由于項目緊張,沒有去扒源碼,找水友要到了答案。原來Edittext光標默認的顏色跟背景色一樣了,導致看不到了。所以產生以下解決方案,有跟蹤過源碼的兄弟希望在評論區留言。
解決辦法:在布局文件中指定androd:textCursorDrawable,如果需要設置成與字體一樣的顏色,該屬性設置為“@null”即可。如果要自定義顏色,需要自定義一個drawable文件,例如:在drawable下創建custom_cursor.xml,內容如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
<size android:width="0.5dp"/>
</shape>
然后,設置android:textCursorDrawable="@drawable/custom_cursor",光標顏色就可以改變為指定顏色.