设置EditText光标颜色

本文翻译自:Set EditText cursor color

I am having this issue where I am using the Android's Holo theme on a tablet project. 我有这个问题,我在平板电脑项目中使用Android的Holo主题。 However, I have a fragment on screen which has a white background. 但是,我在屏幕上有一个具有白色背景的片段。 I am adding an EditText component on this fragment. 我在这个片段上添加了一个EditText组件。 I've tried to override the theme by setting the background of the Holo.Light theme resources. 我试图通过设置Holo.Light主题资源的背景来覆盖主题。 However, my text cursor (carat) remains white and hence, invisible on screen (I can spot it faintly in the edittext field..). 但是,我的文本光标(克拉)仍然是白色的,因此在屏幕上看不见(我可以在编辑文本字段中隐约发现它...)。

Does anyone know how I can get EditText to use a darker cursor color? 有谁知道如何让EditText使用更暗的光标颜色? I've tried setting the style of the EditText to "@android:style/Widget.Holo.Light.EditText" with no positive result. 我已经尝试将EditText的样式设置为"@android:style/Widget.Holo.Light.EditText" ,没有任何正面结果。


#1楼

参考:https://stackoom.com/question/UN3C/设置EditText光标颜色


#2楼

It appears as if all the answers go around the bushes. 似乎所有的答案都围绕着灌木丛。

In your EditText , use the property: EditText ,使用属性:

android:textCursorDrawable="@drawable/black_cursor"

and add the drawable black_cursor.xml to your resources, as follows: 并将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>

This is also the way to create more diverse cursors, if you need. 如果需要,这也是创建更多样化游标的方法。


#3楼

In Layout 在布局中

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

Then create drawable xml: 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>

You have a white color cursor on EditText property. EditText属性上有一个白色光标。


#4楼

Its even easier than that. 它比这更容易。

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

This works in ICS and beyond. 这适用于ICS及更高版本。 I haven't tested it in other versions. 我没有在其他版本中测试它。


#5楼

There is a new way to change cursor color in latest Appcompact v21 在最新的Appcompact v21中有一种更改光标颜色的新方法
Just change colorAccent in style like this: 只需更改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>

Then apply this style on your app theme or activities. 然后在您的应用主题或活动上应用此样式。

Update : this way only works on API 21+ 更新 :这种方式仅适用于API 21+
Update 2 : I'm not sure the minimum android version that it can work. 更新2 :我不确定它可以工作的最低Android版本。
Tested by android version: 经Android版测试:

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

#6楼

For anyone that needs to set the EditText cursor color dynamically, below you will find two ways to achieve this. 对于需要动态设置EditText光标颜色的任何人,下面你会发现两种方法来实现这一点。


First, create your cursor drawable: 首先,创建你的光标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>

Set the cursor drawable resource id to the drawable you created (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)): 将光标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) {
}

To just change the color of the default cursor drawable, you can use the following method: 要仅更改默认光标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) {
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值