首页 文章

设置EditText光标颜色

提问于
浏览
441

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

有谁知道如何让EditText使用更暗的光标颜色?我已经尝试将EditText的样式设置为 "@android:style/Widget.Holo.Light.EditText" 而没有正面结果 .

15 回答

  • 36

    似乎所有的答案都围绕着灌木丛 .

    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>
    

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

  • 24

    晚了,这是我的回答,

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

    这个答案演示如何改变......底线颜色光标颜色光标指针颜色(我使用我的自定义图像)..........使用应用于Activity主题的样式的EditText .

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

    Example:

    <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主题中设置此样式 .

    Example :

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

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

    我找到了答案:)

    我将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 是关键 .

  • 1

    在布局中

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

    然后创建drawable 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

    在最新的 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
    Update 2 :我不确定它可以工作的最小Android版本 .
    经Android版测试:

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

    对于需要动态设置 EditText 光标颜色的任何人,您将在下面找到两种方法来实现此目的 .


    首先,创建你的光标drawable:

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

    将光标drawable资源id设置为您创建的drawable(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) {
    }
    

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

    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) {
        }
    }
    
  • 374
    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>
    
  • 983

    它比这更容易 .

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

    这适用于ICS及更高版本 . 我没有在其他版本中测试过它 .

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

  • 3

    editcolor.xml

    android:textCursorDrawable="@drawable/editcolor"

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

  • 10

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

  • 6

    哇我真的很晚才参加这个派对,但它已经开展了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来说,考虑某种形式的类别可能会很好!

  • 0

    对我来说,我修改了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 . 当我尝试使用它时,颜色不会改变 .

  • 1

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

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

  • 40

    用这个

    机器人:textCursorDrawable = “@色/白色”

相关问题