android– 以编程方式设置textCursorDrawable

android– 以编程方式设置textCursorDrawable,第1张

概述如果我在XML中添加EditText,我可以设置textCursorDrawable=“@null”:<EditTextandroid:id="@+idxtE3Casecode4"android:layout_width="30dp"android:layout_height="wrap_content"android:layout_alignParentTop="true&qu

如果我在XML中添加EditText,我可以设置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" />

现在我在Java中绘制一个EditText.我想设置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 ?                                

怎么设置它?

解决方法:

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

使用反射设置光标:

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

在您的应用中定义可绘制的光标:

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

另一种方法:

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

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

以上是内存溢出为你收集整理的android – 以编程方式设置textCursorDrawable全部内容,希望文章能够帮你解决android – 以编程方式设置textCursorDrawable所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103314.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存