设置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个回答

7

使用这个

android:textCursorDrawable="@color/white"

3
我的设备中光标消失了,因此请使用可绘制对象代替直接设置颜色。 - 林果皞
如果你想隐藏光标,这将非常有用。如果你这么说,你可能会得到更多的投票! - hmac
这不是正确的解决方案。这会在输入之前使光标消失,在输入过程中改变颜色也不合适。 - Bhavin Solanki

6
我们可以在 Material 主题下如下操作:
<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+上进行了测试。


5
唯一的有效答案应该是改变活动的主题: <item name="colorAccent">#000000</item> 您不应该使用android:textCursorDrawable@null,因为这只涉及到光标本身,而不是下面的固定点,如果您想拖动该光标。主题解决方案是最严肃的方案。

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

1
在 Android 9 上出现了java.lang.NoSuchFieldException: No field mDrawableForCursor。 - zakjma
此解决方案已经不再适用。请查看https://developer.android.com/about/versions/pie/restrictions-non-sdk-interfaces - Rafael Pereira Ramos

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文件并更改color accent颜色,这将影响edittext框中的光标样式。


4

在安卓中,这被称为colorAccent

前往 res -> values -> styles.xml 添加

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

如果不存在。


3

请注意当前Activity / fragment / Dialog中的colorAccent,该属性在Styles中定义... ;) 光标颜色与此相关。


3

editcolor.xml

android:textCursorDrawable="@drawable/editcolor"

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


(Note: 该文本已被翻译为简体中文)

只需移除正在edittext中闪烁的光标。 - Avnish Titoriya

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中: public class myDialog extends Dialog {
private Activity activity;
private someVars;

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

干杯 :)


2
在API 21及以上版本中:
<com.google.android.material.textfield.TextInputLayout                
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:theme="@style/CursorColor">

// In colors.xml
<style name="CursorColor">
    <item name="colorPrimary">@color/black</item>
</style>>

最简单的解决方案! - CoolMind

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