更改edittext光标颜色


74

如何以编程方式更改EditText's 光标的颜色

在android 4.0及更高版本中,光标颜色为白色。并且如果EditTexts的背景也是白色,它就变得不可见。

Answers:


121

在您的EditText属性中,有一个属性 android:textCursorDrawable

现在将其设置为@null

android:textCursorDrawable="@null"

因此,现在您的EditText光标与您的EditText TextColor相同。

引用来自于Set EditText光标的颜色


5
我需要以编程方式而不是基于xml的方式来使用它
-Adem

1
@Adem,如果您在XML中进行了设置,则可以通过编程方式将任何颜色设置为文本颜色。
user363349

3
这对于所问的问题(即如何以编程方式设置)也不起作用。
阿德里安

2
OP希望通过代码而不是xml布局来做到这一点,因此这不是正确的答案
Jonathan

1
@ user370305,这使光标看起来很细,几乎不可见
Dipanshu Awasthi

29

我找到了解决此问题的方法。这不是最大的解决方案,但它可以工作。

不幸的是,我只能将静态颜色用作光标颜色。

首先,我在drawable中定义一个黑色光标

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

接下来,我在布局中定义一个示例EditText。

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textCursorDrawable="@drawable/blackpipe"
         >
    </EditText>

当我想在运行时创建EditText时,请使用以下命令:

AttributeSet editText30AttributeSet = null;
int res = getResources().getIdentifier("edit30", "layout", getPackageName());//edit30 is EditText layout
XmlPullParser parser = getResources().getXml(res);
int state=0;
do {
    try {
        state = parser.next();
    } catch (Exception e1) {
        e1.printStackTrace();
    }       
    if (state == XmlPullParser.START_TAG) {
        if (parser.getName().equals("EditText")) {
            editText30AttributeSet = Xml.asAttributeSet(parser);
            break;
        }
    }
} while(state != XmlPullParser.END_DOCUMENT);
EditText view = new EditText(getContext(),editText30AttributeSet);

现在,您有了一个带有黑色光标的EditText视图。也许有人可以改进我的解决方案,以便可以在运行时更改光标。


辛苦了,谢谢!就像自定义EditText组件的超级按钮一样工作。
Divers

17

我认为,这是一种比@Adem发布的解决方案更好的解决方案。

Java:

try {
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}

XML:

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

9

改善Jared Rummler的答案

Java:

try {
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.custom_cursor);
} catch (Exception ignored) {
}

在res / drawable文件夹中创建custom_cursor.xml:

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

XML:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textCursorDrawable="@drawable/custom_cursor"
     >
</EditText>

7

怎么样styles.xml使用程序兼容性-V7?

<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>

为我工作(此示例很简单)。


这些重音是否为您在棒棒糖之前的设备上适用于EditText光标?
安东尼·丘纳德

不,然后(默认)使用默认系统的光标。此后,已经发布了很好的答案,方法更加完善。
shkschneider 2015年

2

您可以将android:textCursorDrawable属性设置为@null,将导致使用android:textColor用作光标颜色。

属性textCursorDrawable在API级别12和更高版本中可用...

谢谢...


2

通过AppTheme设置

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:spinnerStyle">@style/spinner_style</item>
    <!--Add This-->
    <item name="editTextStyle">@style/EdittextStyle</item>
    <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
</style>

<!--New EditText Style-->
<style name="EdittextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:background">?attr/editTextBackground</item>
    <item name="android:textColor">?attr/editTextColor</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <!--<item name="android:textCursorDrawable">@drawable/abc_text_cursor_material</item>-->
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>

</style>

1

因此,这是最终版本-不需要XML,而是以@null的方式进行编程,但可以:

try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set((TextView)yourEditText, 0);
} catch (Exception ignored) {
}

唯一的问题是,它可能会在一天内停止使用某些新版本的Android。

PS我在4.1.2到5.1上进行了测试。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.