更改edittext光标颜色

Ade*_*dem 71 user-interface android

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

在android 4.0及以上版本中,光标颜色为白色.如果EditTexts背景也是白色,则它变得不可见.

use*_*305 115

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

现在将其设置为@null,

android:textCursorDrawable="@null"

所以现在你的EditText光标与EditText TextColor相同.

参考来自设置EditText光标颜色

  • 我需要以编程方式,而不是基于xml (5认同)
  • 这对于提出的问题也不起作用,这是如何以编程方式*设置它. (3认同)
  • OP希望通过代码而不是xml布局来实现,因此这不是正确的答案 (2认同)
  • @user370305 这使光标看起来很薄,几乎不可见 (2认同)

Ade*_*dem 28

我找到了解决这个问题的方法.它不是最好的解决方案,但它确实有效.

不幸的是,我只能使用静态颜色作为光标颜色.

首先,我在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>
Run Code Online (Sandbox Code Playgroud)

接下来,我在布局中定义一个示例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>
Run Code Online (Sandbox Code Playgroud)

当我想在运行时创建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);
Run Code Online (Sandbox Code Playgroud)

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


Jar*_*ler 16

我认为,这是比@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) {
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


kc *_*ili 8

改进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.cursor);
} catch (Exception ignored) {
}
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


shk*_*der 7

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

<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
Run Code Online (Sandbox Code Playgroud)

适合我(这个例子很简单).