设置EditText光标颜色

622
我遇到了这个问题,在平板项目上使用Android的Holo主题。然而,我在屏幕上有一个白色背景的片段。我在这个片段上添加了一个EditText组件。我尝试通过设置Holo.Light主题资源的背景来覆盖主题。然而,我的文本光标(插入符号)仍然是白色的,因此在屏幕上看不见(我可以在编辑文本字段中轻微地看到它)。
有人知道怎么让EditText使用较暗的光标颜色吗?我尝试将EditText的样式设置为 "@android:style/Widget.Holo.Light.EditText",但没有积极的结果。

2
只需在TextEditText中使用-android:textCursorDrawable="@color/your_color_choice" - Lucky Rana
29个回答

1351

android:textCursorDrawable属性设置为@null应该会使用android:textColor作为光标颜色。

属性"textCursorDrawable"在API 12及以上版本可用。


39
哦,这比为光标召唤可绘制对象来使其变成黑色要高效得多!我非常喜欢 Android,但这是一个极为糟糕的默认行为……有人真的需要被打一顿。 - Yevgeny Simkin
29
安卓3.2及以上版本很糟糕。 - Blundell
7
如果您在清单文件中的target版本号大于3.2,您可以使用它,并且它会被低版本忽略。 - Blundell
5
如果进行设置,似乎会使用提示颜色。至少在4.2版本上是这样。对我来说,从2.3.3到4.4,光标选项没有问题。 - MinceMan
9
在较新版本的 Android 中,这种方法不起作用。取而代之的是,它会显示一个灰色的光标,并干扰高亮功能。请改用 @star18bit 的答案。 - Matthew Bahr
显示剩余8条评论

614

在布局中

<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/color_cursor"
    />

然后创建可绘制的XML:color_cursor。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="3dp" />
    <solid android:color="#FFFFFF"  />
</shape>

你的EditText属性上有一个白色光标。


4
属性“textCursorDrawable”仅适用于API级别12及以上。 - SBotirov
91
这是正确答案。不确定为什么有这么多人认为将光标设置为“@null”似乎是个好主意。 - Greg Ennis
1
以上答案,将textCursorDrawable设置为@null可以得到良好的结果,但光标的方向没有意义。不过,这个答案非常好用。真的非常感谢。 - iroiroys
如果背景是白色呢? - Yog Guru
1
我们可以使用这个解决方案来控制光标的宽度,但是@null会使其变得非常细。 - Rahul Gaur
显示剩余4条评论

94

看起来所有的答案都绕着弯子说。

在你的EditText中,使用属性:

android:textCursorDrawable="@drawable/black_cursor"

并将可绘制项black_cursor.xml添加到您的资源中,方式如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <size android:width="1dp" />
    <solid android:color="#000000"/>
</shape>

如果需要创建更多样化的光标,这也是一种方法。


3
这在Android 4.2 Jelly Bean上不起作用...光标会消失,@null是正确的方法。 - Edgar
@Edgar 这在4.2 JB上对我来说似乎完全可以在主题层面上工作。 - mkuech
Android 4.2.2运行良好。不要忘记在black_cursor.xml中添加 <solid android:color="#000000"/>。 - Andrei Aulaska
属性"textCursorDrawable"仅适用于API级别12及以上。 - SBotirov

80
在最新的Appcompact v21中,有一种新的改变光标颜色的方法。只需像这样在样式中更改colorAccent即可:
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->

    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#088FC9</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#088FC9</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <!-- THIS IS WHAT YOU'RE LOOKING FOR -->
    <item name="colorAccent">#0091BC</item> 
</style>

然后在您的应用程序主题或活动上应用此样式。

更新:此方法仅适用于API 21+
更新2:我不确定它可以工作的最低Android版本。
已通过以下Android版本进行测试:

2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked

2
很抱歉,答案是否定的。我在API 18设备上进行了测试,但它无法工作。 - ductran
1
正在使用我的Sony Experia手机。系统版本为PreLollipop(4.4.4)。同时也在使用AppCompat库。 :) - vida
不适用于API 24 / Android 7.0。 colorAccent 更改例如 EditText 底部的线条,但它不会触及实际的光标颜色。哦,当然不再有“v21”了。 - Neph
3
不要因为想要改变光标的颜色而更改应用程序的口音颜色。请保持原样。 - oziomajnr
@oziomajnr,是的,我们应该将自定义主题应用于片段(例如),而不是整个应用程序。 - CoolMind

46

我找到了答案 :)

我将主题的editText样式设置为:

<item name="android:editTextStyle">@style/myEditText</item>
然后我使用了以下drawable来设置光标:`
<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在这里起着关键作用。


3
可能会对想要制作仅为竖线的光标图形的任何人有用:https://dev59.com/53E85IYBdhLWcg3wr1ke - Andrew Wyld
1
请问如何以编程的方式设置光标颜色? - Anil M H
我在给项目命名为android:editTextStyle时遇到了问题,但将其更改为editTextStyle后问题得以解决。请参见https://dev59.com/VVwY5IYBdhLWcg3wpJL_#34010235。 - shoe

27

对于任何需要动态设置EditText光标颜色的人,以下将介绍两种实现方法。


首先,创建您的光标可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
    <solid android:color="#ff000000" />
    
    <size android:width="1dp" />
    
</shape>

将光标可绘制资源ID设置为您创建的可绘制对象(source)):

try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}

要只改变默认光标可绘制的颜色,您可以使用以下方法:

    public static void setTextCursorColor(TextView textView, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            final Drawable drawable = textView.getTextCursorDrawable();
            drawable.setTint(color);
            textView.setTextCursorDrawable(drawable);
        } else try {
            Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
            fCursorDrawableRes.setAccessible(true);
            int   mCursorDrawableRes = fCursorDrawableRes.getInt(textView);
            Field fEditor            = TextView.class.getDeclaredField("mEditor");
            fEditor.setAccessible(true);
            Object   editor          = fEditor.get(textView);
            Class<?> clazz           = editor.getClass();
            Field    fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
            fCursorDrawable.setAccessible(true);

            Drawable[] drawables = new Drawable[2];
            Resources  res       = textView.getContext().getResources();
            drawables[0] = res.getDrawable(mCursorDrawableRes);
            drawables[1] = res.getDrawable(mCursorDrawableRes);
            drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            fCursorDrawable.set(editor, drawables);
        } catch (final Throwable throwable) {
            if (DEBUG) throw new RuntimeException("can't set text cursor color", throwable);
        }
    }

这让我真的很烦恼。AppCompatEditText已经有了setSupportBackgroundTintList,所以是否很容易添加类似setSupportCursorTintList的东西呢? - Joseph Piché
6
@Jared Rummler,您的解决方案对光标有效,但在选择文本时出现的下拉小图标(有两个)仍保持原来的颜色。您能帮助我解决这个问题吗? - Felipe Ribeiro R. Magalhaes
2
你的第三种方法在Android 9上不起作用,但在以下版本中可以正常使用。 - Muhammed Yalçın Kuru

13

晚来一步,以下是我的答案:

这篇文章适用于那些不想更改父主题中的colorAccent,但希望更改EditText属性的人!

本回答演示了如何使用应用于活动主题的样式更改......

  1. 底线颜色
  2. 光标颜色
  3. 光标指针颜色(我使用了自定义图片)...... EditText 的。

输入图像描述

<android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hey" />

示例:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>
    <item name="android:background">@drawable/edit_text_background</item> // background (bottom line at this case)
    <item name="android:textCursorDrawable">@color/white</item>  // Cursor
    <item name="android:textSelectHandle">@drawable/my_white_icon</item> // For pointer normal state and copy text state
    <item name="android:textSelectHandleLeft">@drawable/my_white_icon</item>
    <item name="android:textSelectHandleRight">@drawable/my_white_icon</item>
</style>

现在创建一个可绘制对象(edit_text_background),添加用于背景的资源 xml!您可以根据自己的需要进行自定义!

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="0dp"
        android:left="-3dp"   
        android:right="-3dp"
        android:top="-3dp">

        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/white"/>
        </shape>
    </item>
    </layer-list>

现在,您已在Activity主题中设置了这个样式。

示例:

在您的Activity中,您有一个主题,请将此自定义editText主题设置为该主题。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Your Theme data -->
    <item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
</style>

<item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this 没有效果。 - act262
它是AppCompatEditText吗? - Charuක
1
哦,我错了。我写了错误的属性“android:editTextSteyle”,正确的属性应该是“editTextStyle”。 - act262
<item name="android:textCursorDrawable">@color/white</item> 不起作用。 - Vlad

10

哇,我来晚了,但是这个问题在17天前有活动。看起来我们需要考虑发布我们正在使用的Android版本的答案,因此目前这个答案适用于Android 2.1及以上版本。请前往RES/VALUES/STYLES并添加以下代码行,您的光标将变为黑色。

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!--<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">-->
    <!-- Customize your theme here. -->
    <item name="colorControlActivated">@color/color_Black</item>
    <!--Sets COLOR for the Cursor in EditText  -->
</style>

你需要在你的RES/COLOR文件夹中加入这行代码。
<color name="color_Black">#000000</color>

为什么晚发帖?考虑到安卓已经成为了一个具有许多头的怪物,可能需要考虑一些分类形式!


属性需要API级别21,而不是7(2.1)。 - shoe

9
Edittext cursor color you want changes your color.
   <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/color_cursor"
    />

然后创建可绘制的xml: color_cursor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="3dp" />
    <solid android:color="#FFFFFF"  />
</shape>

7

对于我来说,我修改了AppTheme和values/colors.xml两个值。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorControlNormal">@color/yellow</item>
    <item name="colorAccent">@color/yellow</item>
</style>

以下是colors.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="yellow">#B7EC2A</color>
</resources>

我将放置在editText样式中的android:textCursorDrawable属性移除为@null。当我尝试使用此功能时,颜色没有改变。


1
是的,那样做可以起作用,但当你只需要改变光标颜色时,你也会改变其他主题元素。 - AlanKley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接