586

我在平板电脑项目上使用 Android 的 Holo 主题时遇到了这个问题。但是,我在屏幕上有一个白色背景的片段。我EditText在这个片段上添加了一个组件。我试图通过设置 Holo.Light 主题资源的背景来覆盖主题。但是,我的文本光标(克拉)保持白色,因此在屏幕上不可见(我可以在 edittext 字段中隐约发现它......)。

有谁知道如何让 EditText 使用较暗的光标颜色?我尝试将 EditText 的样式设置为"@android:style/Widget.Holo.Light.EditText"没有积极的结果。

4

26 回答 26

1296

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

属性“textCursorDrawable”在 API 级别 12 及更高级别中可用

于 2012-02-06T18:29:10.673 回答
576

在布局中

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

然后创建drawalble 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 属性上有一个白色光标。

于 2013-03-15T11:22:08.730 回答
87

似乎所有的答案都在草丛中。

在您的EditText中,使用以下属性:

android:textCursorDrawable="@drawable/black_cursor"

并将drawable添加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>

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

于 2012-05-28T19:59:45.343 回答
71

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 版本。
安卓版本测试:

2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked
于 2015-03-20T02:53:12.247 回答
45

我找到了答案:)

我已将主题的 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是这里的关键。

于 2011-08-30T03:57:59.960 回答
25

对于需要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 设置为您创建的可绘制资源(https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)) :

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

要仅更改默认光标可绘制对象的颜色,可以使用以下方法:

public static void setCursorDrawableColor(EditText editText, int color) {
    try {
        Field fCursorDrawableRes = 
            TextView.class.getDeclaredField("mCursorDrawableRes");
        fCursorDrawableRes.setAccessible(true);
        int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
        Field fEditor = TextView.class.getDeclaredField("mEditor");
        fEditor.setAccessible(true);
        Object editor = fEditor.get(editText);
        Class<?> clazz = editor.getClass();
        Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
        fCursorDrawable.setAccessible(true);

        Drawable[] drawables = new Drawable[2];
        Resources res = editText.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 ignored) {
    }
}
于 2015-09-09T08:47:45.037 回答
13

派对迟到了,这是我的答案,

这适用于不想更改colorAccent其父主题但想要更改EditText属性的人!

这个答案演示了如何改变......

  1. 底线颜色
  2. 光标颜色
  3. 光标指针颜色(我使用了我的自定义图像)............EditText使用应用于 Activity 主题的样式。

在此处输入图像描述

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

现在创建一个drawable( 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 主题中设置此样式一样。

例子 :

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

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Your Theme data -->
    <item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
</style>
于 2017-02-02T00:48:23.107 回答
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"
    />

然后创建drawalble 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>
于 2016-12-07T12:09:41.393 回答
8

哇,我参加这个聚会真的迟到了,但它在 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>

为什么发这么晚?为 Android 已成为的众多头目怪物考虑某种形式的类别可能会很好!

于 2017-08-27T00:39:53.307 回答
7

对我来说,我修改了 AppTheme 和值 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>

我将 android:textCursorDrawable 属性取出到了我放在 editText 样式中的@null。当我尝试使用它时,颜色不会改变。

于 2016-02-22T16:53:22.503 回答
7

用这个

android:textCursorDrawable="@color/white"
于 2017-08-09T11:40:22.097 回答
5

唯一有效的答案应该是更改活动的主题: <item name="colorAccent">#000000</item> 您不应该使用android:textCursorDrawableto,@null因为如果您想拖动该光标,这仅涉及光标本身,而不涉及光标下方的引脚。主题解决方案是最严重的解决方案。

于 2017-10-16T12:05:31.197 回答
5

这里@Jared Rummler 的程序化 setCursorDrawableColor() 版本也适用于 Android 9 Pie。

@SuppressWarnings({"JavaReflectionMemberAccess", "deprecation"})
public static void setCursorDrawableColor(EditText editText, int color) {

    try {
        Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
        cursorDrawableResField.setAccessible(true);
        int cursorDrawableRes = cursorDrawableResField.getInt(editText);
        Field editorField = TextView.class.getDeclaredField("mEditor");
        editorField.setAccessible(true);
        Object editor = editorField.get(editText);
        Class<?> clazz = editor.getClass();
        Resources res = editText.getContext().getResources();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Field drawableForCursorField = clazz.getDeclaredField("mDrawableForCursor");
            drawableForCursorField.setAccessible(true);
            Drawable drawable = res.getDrawable(cursorDrawableRes);
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            drawableForCursorField.set(editor, drawable);
        } else {
            Field cursorDrawableField = clazz.getDeclaredField("mCursorDrawable");
            cursorDrawableField.setAccessible(true);
            Drawable[] drawables = new Drawable[2];
            drawables[0] = res.getDrawable(cursorDrawableRes);
            drawables[1] = res.getDrawable(cursorDrawableRes);
            drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            cursorDrawableField.set(editor, drawables);
        }
    } catch (Throwable t) {
        Log.w(TAG, t);
    }
}
于 2019-03-09T20:21:09.290 回答
5
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item> -- change this one
</style>

转到 styles.xml 并更改颜色口音,这将影响编辑文本框中的光标

于 2020-01-06T01:57:57.387 回答
4

colorAccent这在Android中被调用。

转到 res -> values -> styles.xml 添加

<item name="colorAccent">#FFFFFF</item>

如果不存在。

于 2019-07-09T23:01:08.727 回答
3

编辑颜色.xml

android:textCursorDrawable="@drawable/editcolor"

在xml文件中设置edittext背景颜色的颜色代码

于 2017-04-28T05:20:13.653 回答
3

在 Dialog 中尝试了所有这些技术之后,我终于有了这个想法:将主题附加到 Dialog 本身而不是 TextInputLayout。

<style name="AppTheme_Dialog" parent="Theme.AppCompat.Dialog">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorWhite</item>
    <item name="colorAccent">@color/colorPrimary</item>

</style>

在 onCreate 里面:

公共类 myDialog 扩展 Dialog {

private Activity activity;
private someVars;

public PopupFeedBack(Activity activity){
    super(activity, R.style.AppTheme_Dialog);
    setContentView(R.layout.myView);
    ....}}

干杯:)

于 2020-04-10T14:44:29.197 回答
3

注意当前活动/片段/对话框中的 colorAccent,在样式中定义... ;) 光标颜色与之相关。

于 2020-04-10T14:48:42.443 回答
3

我们可以在物质主题中做到这一点,如下所示:

<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="android:colorControlNormal">#ff0000</item>
    <item name="android:colorControlActivated">#ff0000</item>
    <item name="android:colorControlHighlight">#ff0000</item>
    ...
</style>

如果您也想更改复选框和单选颜色,请添加以下行:

<item name="colorAccent">#ff0000</item>

我已经在 Android API 21+ 中测试过

于 2021-04-23T15:45:00.153 回答
2

另一个简单的解决方案是转到项目文件夹中的 res>values>colors.xml 并将颜色重音的值编辑为您喜欢的颜色

<color name="colorAccent">#000000</color>

上面的代码将您的光标更改为黑色。

于 2019-01-31T06:46:06.257 回答
1

它甚至比这更容易。

<style name="MyTextStyle">
    <item name="android:textCursorDrawable">#000000</item>
</style>

这适用于 ICS 及其他领域。我没有在其他版本中测试过。

于 2013-06-11T23:05:30.043 回答
1

你想要特定的颜色,你必须使用AppCompatEditText然后背景设置为null

为我工作

<android.support.v7.widget.AppCompatEditText
    android:background="@null"
    android:textCursorDrawable="@color/cursorColor"/>

看到这个要点

于 2018-12-19T07:56:26.460 回答
0

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#36f0ff</item>
    <item name="colorPrimaryDark">#007781</item>
    <item name="colorAccent">#000</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

修改styles.xm中colorAccent的颜色,就这么简单

于 2016-08-25T14:26:14.810 回答
0

如果使用样式和实现

颜色控制激活

替换它的颜色/白色以外的值。

于 2019-09-19T01:19:13.330 回答
-1

您可以在布局中使用下面的代码

android:textCursorDrawable="@color/red"
        android:textColor="@color/black
于 2019-09-27T16:05:49.600 回答
-1
<style name="CustomTheme" parent="AppTheme">
    <item name="colorAccent">@color/yourColor</item>
</style>

在样式中添加它,然后在 Edittext 中设置主题,如下所示:

android:theme="@style/CustomTheme"

就是这样!

于 2022-01-19T18:15:07.140 回答