android 改變TextView和EditText文字選中效果

來源:互聯網
上載者:User

歡迎轉帖,但請註明地址: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();        }    }

於是我們只要設定這個方法就可以了

效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.