Android changes textview and edittext Text Selection

Source: Internet
Author: User

Please post, but please note the address: http://blog.csdn.net/ethan_xue/ Thank you

Recently, due to project requirements, the image selected when edittext is changed and the network search fails, so you can view the source code.

There is nothing in the edittext code, so I guess that in the textview of its parent class, I soon found such a piece of code

  int mTextSelectHandleLeftRes;    int mTextSelectHandleRightRes;    int mTextSelectHandleRes;    Drawable mSelectHandleLeft;    Drawable mSelectHandleRight;    Drawable mSelectHandleCenter;

The code written by Google is pretty. Find This code according to mtextselecthandleleftres.

 case com.android.internal.R.styleable.TextView_textSelectHandleLeft:      mTextSelectHandleLeftRes = a.getResourceId(attr, 0);      break;

You can see R. styleable. textview_textselecthandleleft and choose to view the res file.

In 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>

See? Android: ATTR/textselecthandleleft to view ATTR. xml. The result is as follows:

<!-- 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" />

This attribute can only be described here. The cup has only one style. Where is the image file? Therefore, using the powerful notepad ++ search function, CTRL + f -- file search -- textselecthandleleft found public. in XML and themes. themes. this is what is written in 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>

The image is finally found. The effect after replacement

Then we can see that the selected word background is yellow. How can we change it?

Source code

 /**     * 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();        }    }

So we only need to set this method.

Effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.