您的位置:首页 > 移动开发 > Android开发

android 改变TextView和EditText文字选中效果

2012-10-28 20:59 555 查看
欢迎转帖,但请注明地址: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();
        }
    }
于是我们只要设置这个方法就可以了

效果如图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: