【问题标题】:Set EditText cursor color设置 EditText 光标颜色
【发布时间】:2011-11-06 11:55:03
【问题描述】:

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

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

【问题讨论】:

    标签: android android-edittext android-styles


    【解决方案1】:

    我找到了答案:)

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

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 哦,伙计,这比将光标变为黑色的可绘制对象要高效得多!我爱死安卓了,但这是一个非常糟糕的默认行为......真的需要有人为此受到打击。
    • android 3.2 及更高版本.. 烂
    • 虽然如果你在你的清单中target >3.2 你可以使用它,它会被低版本忽略
    • 如果您设置它,似乎会使用提示颜色。至少在 4.2 上。光标选项对我来说从 2.3.3-4.4 没有问题
    • 这在最新版本的 android 中不起作用。相反,它显示一个灰色光标并与突出显示功能混淆。请改用@star18bit 的答案。
    【解决方案3】:

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

    在您的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>
    

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

    【讨论】:

    • 这在 Android 4.2 Jelly Bean 上不起作用...光标消失,@null 是要走的路。
    • @Edgar 这似乎对我在 4.2 JB 上的主题级别很好。
    • Android 4.2.2 工作正常。不要忘记在 black_cursor.xml 中添加
    • 属性“textCursorDrawable”仅在 API 级别 12 及更高级别中使用
    【解决方案4】:

    在布局中

    <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 属性上有一个白色光标。

    【讨论】:

    • 属性“textCursorDrawable”仅在 API 级别 12 及更高级别中使用
    • 这是正确答案。不知道为什么这么多人认为将光标设置为“@null”似乎是个好主意
    • 以上答案,将 textCursorDrawable 设置为 @null 结果看起来不错,但光标的方向没有意义,但是,这个答案真的很棒。真的谢谢
    • 这是最好的解决方案。
    • 我们可以使用这个解决方案来控制光标的宽度,但是@null 让它变得很细
    【解决方案5】:

    它比这更容易。

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

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

    【讨论】:

    • 当心,如果您打算将此样式应用于您的 EditText(这没有父样式,这应该是与您的主题相关的默认样式),您将失去所有其他样式来自Holo 主题。
    • 不是和“@null”值一样吗?
    • 您可以在视图中添加样式 juste,它会覆盖活动的主题。
    【解决方案6】:

    在最新的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
    

    【讨论】:

    • 恐怕答案是否定的。我在 API 18 设备上进行了测试,但它不起作用。
    • 在我的 Sony Experia 上工作。 PreLollipop (4.4.4)。还使用 AppCompat 库。 :)
    • 不适用于 API 24/Android 7.0。 colorAccent 更改,例如EditText 底部的行,但它不会触及实际的光标颜色。哦,当然不再有“v21”了。
    • 您不应该更改应用程序的强调色,因为您想更改光标的颜色。
    • @oziomajnr,是的,我们应该将自定义主题应用于例如片段,而不是整个应用程序。
    【解决方案7】:

    对于需要动态设置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">来源)):

    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) {
        }
    }
    

    【讨论】:

    • 这真的让我很困扰。 AppCompatEditText 已经有了setSupportBackgroundTintList,那么添加类似setSupportCursorTintList 这样的东西不是很简单吗?
    • @Jared Rummler 您的解决方案适用于光标,但出现在光标下方的水滴(当您选择文本时,它会出现两个)仍然是原始颜色。你能帮我解决这个问题吗?
    • 您的 3. 方法在 Android 9 中不起作用,但适用于以下版本。
    【解决方案8】:

    对我来说,我修改了 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 属性取出到 @null 中,并放在 editText 样式中。当我尝试使用它时,颜色不会改变。

    【讨论】:

    • 是的,但是当您可能只需要更改光标颜色时,您还可以更改其他主题元素。
    【解决方案9】:

    <!-- 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的颜色,就这么简单

    【讨论】:

      【解决方案10】:
      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>
      

      【讨论】:

        【解决方案11】:

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

        这适用于希望更改其父主题中的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>
        

        【讨论】:

        • &lt;item name="editTextStyle"&gt;@style/AppTheme.EditText&lt;/item&gt; // inculude this 无效
        • 是 AppCompatEditText 吗?
        • 哦,我错了。我写错了 attr android:editTextSteyle ,正确的 attr 是 editTextStyle
        • &lt;item name="android:textCursorDrawable"&gt;@color/white&lt;/item&gt; 不起作用
        【解决方案12】:

        editcolor.xml

        android:textCursorDrawable="@drawable/editcolor"

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

        【讨论】:

        • 只需删除在edittext中闪烁的corsor
        【解决方案13】:

        使用这个

        android:textCursorDrawable="@color/white"
        

        【讨论】:

        • 光标在我的设备中消失了,所以使用drawable而不是直接设置颜色。
        • 如果你想隐藏光标,这很有用,如果你说你可能会得到更多的选票!
        【解决方案14】:

        哇,我真的迟到了,但它在 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 已成为的众多头目怪物考虑某种形式的类别可能会很好!

        【讨论】:

        • 属性需要 API 级别 21 而不是 7 (2.1)
        【解决方案15】:

        唯一有效的答案应该是改变活动的主题: &lt;item name="colorAccent"&gt;#000000&lt;/item&gt; 你不应该使用android:textCursorDrawable@null,因为如果你想拖动光标,这只涉及光标本身,而不是光标下方的引脚。主题解决方案是最严重的。

        【讨论】:

          【解决方案16】:

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

          为我工作

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

          this gist

          【讨论】:

            【解决方案17】:

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

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

            上面的代码将光标变为黑色。

            【讨论】:

            • 谢谢,这正是我想要的
            【解决方案18】:

            @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);
                }
            }
            

            【讨论】:

            【解决方案19】:

            这在 Android 中称为 colorAccent

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

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

            如果不存在。

            【讨论】:

              【解决方案20】:

              如果使用样式并实现

              颜色控制激活

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

              【讨论】:

                【解决方案21】:

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

                android:textCursorDrawable="@color/red"
                        android:textColor="@color/black
                

                【讨论】:

                  【解决方案22】:
                  <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 并更改颜色重音,这将影响编辑文本框中的光标

                  【讨论】:

                    【解决方案23】:

                    在 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 扩展对话框 {

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

                    干杯:)

                    【讨论】:

                      【解决方案24】:

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

                      【讨论】:

                        【解决方案25】:

                        我们可以在有韵主题中这样做:

                        <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+ 中测试过

                        【讨论】:

                          【解决方案26】:
                          <style name="CustomTheme" parent="AppTheme">
                              <item name="colorAccent">@color/yourColor</item>
                          </style>
                          

                          在样式中添加这个,然后在 Edittext 中设置主题如下:

                          android:theme="@style/CustomTheme"
                          

                          就是这样!

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 2012-07-23
                            • 2011-12-25
                            • 2015-05-23
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多