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

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


我找到了答案:)

我已经设置了主题的editText样式为:

<item name="android:editTextStyle">@style/myEditText</item>

然后我使用下面的绘图来设置光标:

`

<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是这里的键。

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

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

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

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

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

在布局

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

其实比这更简单。

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

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

在最新的Appcompact v21中,有一种改变光标颜色的新方法 只需要改变颜色,就像这样:

 <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版本测试:

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

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

对我来说,我修改了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样式。当我尝试使用这个,颜色不会改变。

<!-- 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" />

改变颜色的颜色风格的口音。Xm,很简单

Edittext cursor color you want changes your color.
   <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>

迟到了,这是我的答案,

这是为那些不希望更改父主题中的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>

editcolor.xml

android: textCursorDrawable =“@drawable / editcolor”

在xml文件中设置编辑文本背景颜色的颜色代码

使用这个

android:textCursorDrawable="@color/white"

哇,我真的很晚才参加这个派对,但它在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 " > # 000000 > < /项目 你不应该使用android:textCursorDrawable @null,因为这只关心光标本身,而不是针下面的光标,如果你想拖动该光标。主题化解决方案是最重要的解决方案。

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

对我有用

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

看看这个要点

另一个简单的解决方案是去res>values>colors.xml在你的项目文件夹和编辑的颜色重音值为您喜欢的颜色

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

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

这里@Jared Rummler的编程setCursorDrawableColor()版本也适用于Android 9派。

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

在Android中叫做colorAccent。

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

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

If不存在。

如果使用样式和实现

colorControlActivate

将其值替换为其他颜色/白色。

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

android:textCursorDrawable="@color/red"
        android:textColor="@color/black
<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并更改颜色重音,这将影响编辑文本框的光标

在花了很多时间在一个对话框中尝试所有这些技术之后,我终于有了这个想法:将主题附加到对话框本身,而不是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扩展Dialog {

private Activity activity;
private someVars;

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

欢呼:)

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

我们可以在材料主题上做如下:

<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+中进行了测试

<style name="CustomTheme" parent="AppTheme">
    <item name="colorAccent">@color/yourColor</item>
</style>

将其添加到样式中,然后在Edittext中设置主题,如下所示:

android:theme="@style/CustomTheme"

就是这样!

api21及以上版本:

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