歡迎轉帖,但請註明地址:http://blog.csdn.net/ethan_xue/ 謝謝
最近因為項目需要,更改EditText被選中時的圖片,網路搜尋未果,於是查看原始碼
EditText代碼裡沒什麼東西,於是猜測在其父類TextView中,很快發現這樣一段代碼
int mTextSelectHandleLeftRes; int mTextSelectHandleRightRes; int mTextSelectHandleRes; Drawable mSelectHandleLeft; Drawable mSelectHandleRight; Drawable mSelectHandleCenter;
看名字就猜到是這個了,google寫的代碼就是漂亮,根據mTextSelectHandleLeftRes找到這段代碼
case com.android.internal.R.styleable.TextView_textSelectHandleLeft: mTextSelectHandleLeftRes = a.getResourceId(attr, 0); break;
看到R.styleable.TextView_textSelectHandleLeft,果斷去查看res檔案,呵呵,果然就是他
styles.xml裡
<style name="Widget.TextView"> <item name="android:textAppearance">?android:attr/textAppearanceSmall</item> <item name="android:textSelectHandleLeft">?android:attr/textSelectHandleLeft</item> <item name="android:textSelectHandleRight">?android:attr/textSelectHandleRight</item> <item name="android:textSelectHandle">?android:attr/textSelectHandle</item> </style>
看到?android:attr/textSelectHandleLeft去查看attr.xml,結果有以下代碼
<!-- Reference to a drawable that will be used to display a text selection anchor on the left side of a selection region. --> <attr name="textSelectHandleLeft" />
到此只能說明有這個屬性,那杯具了,只有個style,圖片檔案在哪裡啊?於是使用強大的notepad++的尋找功能,ctrl+f--檔案尋找--textSelectHandleLeft結果發現public.xml裡和themes.xml裡都有,themes.xml裡是這樣寫的
<!-- Text selection handle attributes --> <item name="textSelectHandleLeft">@android:drawable/text_select_handle_left</item> <item name="textSelectHandleRight">@android:drawable/text_select_handle_right</item> <item name="textSelectHandle">@android:drawable/text_select_handle_middle</item> <item name="textSelectHandleWindowStyle">@android:style/Widget.TextSelectHandle</item>
到此終於找到圖片了,替換掉後的效果
然後看到選中的字的背景是黃色的,怎樣改變呢?
源碼裡
/** * Sets the color used to display the selection highlight. * * @attr ref android.R.styleable#TextView_textColorHighlight */ @android.view.RemotableViewMethod public void setHighlightColor(int color) { if (mHighlightColor != color) { mHighlightColor = color; invalidate(); } }
於是我們只要設定這個方法就可以了
效果