ChatGPT解决这个技术问题 Extra ChatGPT

设置 EditText 光标颜色

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

有谁知道如何让 EditText 使用较暗的光标颜色?我尝试将 EditText 的样式设置为 "@android:style/Widget.Holo.Light.EditText",但没有得到肯定的结果。

只需在您的 TextEditText 内使用 - android:textCursorDrawable="@color/your_color_choice"

P
Paul Verest

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

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


哦,伙计,这比将光标变为黑色的可绘制对象更有效!我爱死 Android,但这是一个非常糟糕的默认行为......真的需要有人为此受到打击。
android 3.2及更高版本..很烂
尽管如果您的清单中的 target > 3.2 您可以使用它,但对于较低版本,它将被忽略
如果您设置它,似乎会使用提示颜色。至少在 4.2 上。从 2.3.3-4.4 的光标选项对我来说没有问题
这在最新版本的 android 中不起作用。相反,它显示一个灰色光标并与突出显示功能混淆。请改用@star18bit 的答案。
D
DPP

在布局中

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

您在 EditText 属性上有一个白色光标。


属性“textCursorDrawable”仅用于 API 级别 12 及更高级别
这是正确的答案。不知道为什么这么多人认为将光标设置为“@null”似乎是个好主意
上面的答案,将 textCursorDrawable 设置为 @null 结果看起来不错,但是光标的方向没有意义,但是,这个答案真的很棒。真的谢谢
我们可以使用具有不同颜色的相同excel作为光标吗?
我们可以使用此解决方案控制光标的宽度,但 @null 使其非常细
L
Lonzak

似乎所有的答案都在草丛中。

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

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


这在 Android 4.2 Jelly Bean 上不起作用......光标消失了,@null 是要走的路。
@Edgar 这似乎对我在 4.2 JB 的主题级别上工作得很好。
Android 4.2.2 工作正常。不要忘记在 black_cursor.xml 中添加
属性“textCursorDrawable”仅用于 API 级别 12 及更高级别
d
ductran

在最新的 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+ 更新 2:我不确定它可以工作的最低 android 版本。安卓版本测试:

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

恐怕答案是否定的。我在 API 18 设备上进行了测试,但它不起作用。
在我的 Sony Experia 上工作。 PreLollipop (4.4.4)。还使用 AppCompat 库。 :)
不适用于 API 24/Android 7.0。 colorAccent 更改,例如 EditText 底部的行,但它不会触及实际的光标颜色。哦,当然不再有“v21”了。
您不应该更改应用程序的强调色,因为您想更改光标的颜色。
@oziomajnr,是的,我们应该将自定义主题应用于例如片段,而不是整个应用程序。
S
Sandeep P

我找到了答案:)

我已将主题的 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 是这里的关键。


stackoverflow.com/questions/2658772/android-vertical-line-xml 可能对任何想要使光标可绘制但只是一条垂直线的人有用:)
你能告诉我们如何以编程方式设置光标颜色吗
我将项目命名为 android:editTextStyle 时遇到了问题,但将其更改为 editTextStyle 解决了这个问题。请参阅stackoverflow.com/a/34010235/6744473
p
pevik

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

这真的让我很困扰。 AppCompatEditText 已经有 setSupportBackgroundTintList,那么添加类似 setSupportCursorTintList 的内容不是很简单吗?
@Jared Rummler 您的解决方案适用于光标,但出现在光标下方的液滴(当您选择文本时,它出现两个)仍然是原始颜色。你能帮我解决这个问题吗?
您的 3. 方法在 Android 9 中不起作用,但适用于以下版本。
C
Charuක

派对迟到了,这是我的答案,

这适用于那些希望更改其父主题中的 colorAccent,但希望更改 EditText 属性的人!

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

https://i.stack.imgur.com/92Nt6.png

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

现在创建一个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 主题中设置此样式一样。

例子 :

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

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

<item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this 无效
是 AppCompatEditText 吗?
哦,我错了。我写错了 attr android:editTextSteyle,正确的 attr 是 editTextStyle
<item name="android:textCursorDrawable">@color/white</item> 不起作用
A
AMAN SINGH
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>

V
Vector

哇,我参加这个聚会真的迟到了,但它在 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 已成为的众多头目怪物考虑某种形式的类别可能会很好!


属性需要 API 级别 21 而不是 7 (2.1)
E
Emily Alexandra Conroyd

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


是的,但是当您可能只需要更改光标颜色时,您还可以更改其他主题元素。
K
Kishan Viramgama

用这个

android:textCursorDrawable="@color/white"

光标在我的设备中消失了,所以使用可绘制而不是直接设置颜色。
如果您想隐藏光标,这很有用,如果您说您可能会获得更多选票!
这不是适当的解决方案。这将在键入之前和键入之间消失光标,如果我们更改颜色,则不适合显示。
J
JPhi Denis

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


O
Oliver Jonas

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

java.lang.NoSuchFieldException: No field mDrawableForCursor 在 Android 9 上。
此解决方案不再起作用。请参阅developer.android.com/about/versions/pie/…
d
dave o grady
<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 并更改颜色口音,这将影响编辑文本框中的光标


N
Niamatullah Bakhshi

在 Android 中称为 colorAccent

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

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

如果不存在。


p
protanvir993

我们可以在物质主题中做到这一点,如下所示:

<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+ 中测试过


J
John Joe

编辑颜色.xml

android:textCursorDrawable="@drawable/editcolor"

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


只需删除在edittext中闪烁的corsor
A
Arnaud

在 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 里面:

公共类 myDialog 扩展 Dialog {

private Activity activity;
private someVars;

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

干杯:)


A
Arnaud

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


K
Kenneth Murerwa

另一个简单的解决方案是转到项目文件夹中的 res>values>colors.xml 并将颜色重音的值编辑为您喜欢的颜色

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

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


谢谢,这正是我想要的
C
Clark Will Battle

它甚至比这更容易。

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

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


请注意,如果您打算将此样式应用于您的 EditText(这没有父样式,这应该是与您的主题相关的默认样式),您将失去来自 Holo 主题的所有其他样式。
与“@null”值不一样吗?
您可以将样式添加到视图中,该样式会覆盖活动的主题。
d
denizs

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

为我工作

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

请参阅this gist


I
Iggy88

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

A
Abhishek Saini

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


l
latifalbr

如果使用样式和实现

颜色控制激活

替换它的颜色/白色以外的值。


T
Tienanhvn

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

android:textCursorDrawable="@color/red"
        android:textColor="@color/black

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

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

android:theme="@style/CustomTheme"

就是这样!