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

有人知道如何让EditText使用更深的光标颜色吗?我已经尝试将EditText的样式设置为“@android:style/Widget.Holo.Light”。没有积极的结果。


当前回答

似乎所有的答案都在灌木丛中流传。

在你的EditText中,使用属性:

android:textCursorDrawable="@drawable/black_cursor"

并添加可绘制的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>

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

其他回答

唯一有效的答案应该是改变活动的主题: <项目名称= " colorAccent " > # 000000 > < /项目 你不应该使用android:textCursorDrawable @null,因为这只关心光标本身,而不是针下面的光标,如果你想拖动该光标。主题化解决方案是最重要的解决方案。

在布局

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

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

哇,我真的很晚才参加这个派对,但它在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已经变成了一头怪物,考虑一些分类形式可能会很好!

迟到了,这是我的答案,

这是为那些不希望更改父主题中的colorAccent,但想更改EditText属性的人准备的!

这个答案演示了如何更改...... 底线颜色 光标的颜色 光标指针颜色(我使用我的自定义图像)..........使用样式应用到活动主题的编辑文本。

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

现在创建一个可绘制的(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>

现在,正如您在活动主题中设置了此样式。

例子:

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

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

对于任何需要动态设置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) {
    }
}