Android 以编程方式设置 textCursorDrawable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11554078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:01:18  来源:igfitidea点击:

set textCursorDrawable programmatically

androidandroid-edittext

提问by D T

If I add an EditTextin XML I can set textCursorDrawable="@null":

如果我EditText在 XML 中添加一个,我可以设置textCursorDrawable="@null"

<EditText
    android:id="@+id/txtE3Casecode4"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#C7C7C5"
    android:textCursorDrawable="@null"
    android:ems="10"
    android:inputType="number"
    android:maxLength="2"
    android:text="01"
    android:textColor="#000000" />

Now I draw an EditTextin Java. I want set android:textCursorDrawable="@null".

现在我EditText用Java画一个。我要设置android:textCursorDrawable="@null"

LinearLayout.LayoutParams paramtext = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
EditText txtOther = new EditText(this);
txtOther.setLayoutParams(paramtext);
txtOther.setBackgroundColor(Color.WHITE);
txtOther.setTextColor(Color.BLACK);
txtOther.setId(99999);
// txtOther.setCursorDrawable ?                                

How do set it?

怎么设置?

回答by Jared Rummler

There is no public API to set the cursor drawable. You can set it programmatically by using reflection. The field mCursorDrawableReshasn't changed so this should work on all devices, unless a manufacturer changed something or it is later changed.

没有公共 API 来设置光标可绘制。您可以使用反射以编程方式设置它。该字段mCursorDrawableRes没有更改,因此这应该适用于所有设备,除非制造商更改了某些内容或稍后更改。

Use reflection to set the cursor:

使用反射设置光标:

EditText yourEditText = new EditText(context);

...

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) {
}

Define a cursor drawable in your app:

在你的应用中定义一个可绘制的光标:

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


Another approach:

另一种方法:

You can also set the cursor color with the following method:

您还可以使用以下方法设置光标颜色:

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];
        drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
        drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
        drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
        drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
        fCursorDrawable.set(editor, drawables);
    } catch (Throwable ignored) {
    } 
} 

回答by Hyukjae

Using reflection for the cursor drawable is prohibited from Android Q.

Android Q 禁止对光标绘制使用反射。

Instead, there is a new setTextCursorDrawable APIand we can use this.

相反,有一个新的setTextCursorDrawable API,我们可以使用它。

回答by cinsondev

the answer is ---------------------> It's unattainable.

答案是---------------------> 遥不可及。

Actually, I also met this problem in my work, but after checking google's doc and source code, I fond that you cannot set this attribute in java code.

其实我在工作中也遇到过这个问题,但是在查看了google的文档和源码后,我喜欢你不能在java代码中设置这个属性。

In TextViewclass you can find the code below

TextView课堂上你可以找到下面的代码

case com.android.internal.R.styleable.TextView_textCursorDrawable:
    mCursorDrawableRes = a.getResourceId(attr, 0);
    break;

but the method textCursorDrawable()is only existed in R.attr, if you wanna set this attribute, you can only call the construct method below by including a editText in XML file.

但是该方法textCursorDrawable()只存在于 中R.attr,如果你想设置这个属性,你只能通过在 XML 文件中包含一个 editText 来调用下面的构造方法。

public EditText(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.editTextStyle);
}

回答by Mr-IDE

If you use the AppCompat libraryand your Activity extends AppCompatActivity, you can set the color of the text cursor (for all EditTexts) using an XML style of colorAccent:

如果您使用AppCompat 库并且您的 Activity 扩展了 AppCompatActivity,则可以使用 XML 样式colorAccent设置文本光标的颜色(对于所有 EditTexts):

<style name="AppTheme" parent="@style/Theme.AppCompat">
    <item name="colorAccent">#FF4081</item>
</style>

This works on Android 5+ and is backwards compatible with older Android versions from Android 4.4 (API 19) to Android 4.0 (API 14). Note: If you're using an AutoCompleteTextView, ensure that any custom subclass of it extends AppCompatAutoCompleteTextView, otherwise the text cursor styling will not work.

这适用于 Android 5+,并向后兼容从 Android 4.4 (API 19) 到 Android 4.0 (API 14) 的旧 Android 版本。注意:如果您使用 AutoCompleteTextView,请确保它的任何自定义子类扩展 AppCompatAutoCompleteTextView,否则文本光标样式将不起作用。

See also this guide, for how to apply the color accent (tint) to Dialog buttons: Android v21 Theme.Appcompat color accent is ignored, no padding on dialogs

另请参阅本指南,了解如何将颜色强调(色调)应用于对话框按钮:Android v21 Theme.Appcompat 颜色强调被忽略,对话框上没有填充