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

2
另一个简单的解决方案是进入项目文件夹中的res>values>colors.xml,将颜色强调的值编辑为您喜欢的颜色。"最初的回答"
<color name="colorAccent">#000000</color>

以上代码将把你的光标变成黑色。


谢谢,这正是我在寻找的。 - Shivam Jha

1
这甚至比那更容易。

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

这在ICS及以上版本中有效。我没有在其他版本中进行过测试。


3
请注意,如果您要将此样式应用于EditText(它没有父样式,这应该是与您的主题相关的默认样式),则会失去来自Holo主题的所有其他样式。 - Varun
这不就是 "@null" 值吗? - Paul Verest
你可以为一个视图添加样式,这将覆盖活动的主题。 - JPhi Denis

1
如果您想使用特定颜色,您必须使用AppCompatEditText,然后将背景设置为null
对我有效。
<android.support.v7.widget.AppCompatEditText
    android:background="@null"
    android:textCursorDrawable="@color/cursorColor"/>

请看此代码片段:this gist

1

这是我解决的方法

步骤1:更新最新资料

implementation 'com.google.android.material:material:1.1.0'

第二步:输入参考 TextInputLayout.OutlinedBox.Dense,任何...
    <style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/app_color_main</item>
        <item name="hintTextColor">@color/app_color_main</item>
        <item name="android:theme">@style/exampleCursor</item>
    </style>

第三步:[更改光标颜色很重要] - 将colorControlActivated属性设置为所需的光标颜色。
    <style name="exampleCursor" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
        <item name="colorControlActivated">@color/app_color_main</item>
    </style>

更多细节请点击这里

1
2023 - 《Compose》 版本
你可以使用 cursorBrush 来改变光标的颜色。
示例用法
BasicTextField(
            
            ...
            cursorBrush = SolidColor(Transparent),
            ...
        )

0

<!-- 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 的颜色,很简单。


0

如果使用样式和实现

colorControlActivate

替换它的值为其他颜色/白色之外的颜色。


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

将以下内容添加到样式中,然后在Edittext中将主题设置为:

android:theme="@style/CustomTheme"

就是这样!


-2

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

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

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