Change the color of the cursor of an EditText in Android 3.0

点点圈 提交于 2019-12-05 00:53:55

I found the answer :)

I've set the Theme's editText style to:

<item name="android:editTextStyle">@style/myEditText</item>

Then I've used the following drawable to set the cursor:

<style name="myEditText" parent="@android:style/Widget.Holo.Light.EditText"> 
<item name="android:background">@android:drawable/editbox_background_normal</item> 
<item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item> 
<item name="android:height">40sp</item> </style>

android:textCursorDrawable is the key here.

And also refer this one Vertical line using XML drawable

I was trying to change the cursor color in my app which targeted API 8. I've found out that TextView uses textColor property as the cursor color. Here's a part of onDraw() defined in TextView API 8:

    int color = mCurTextColor;

    if (mLayout == null) {
        assumeLayout();
    }

    Layout layout = mLayout;
    int cursorcolor = color;

The cursorcolor is then used to constract an android.graphics.Path object representing the color.

If you need to change this behaviour, you're up to quite a task, you'll have to implement your own TextView.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!