Android EditText处理光标不显示问题(兼容华为,荣耀,小米)

解决国产自定义Room EditText无法修改光标以及光标不展示问题

产生原因:

1.某些机型光标默认是白色 当EditText是白色背景的时候会导致光标展示不出来

2.光标设置了默认不展示也会导致光标展示不出来

3.某些机型修改了系统方法引发的光标展示不出来

解决方案:

1:设置EditText属性

 <EditText
            android:layout_centerInParent="true"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColor="@color/color_333"
            android:textColorHint="@color/color_999"

            android:background="@null"
            android:hint="请输入数据"
            //设置 光标展示 部分机型设置会默认不展示
            android:cursorVisible="true"
             //设置 光标展示的资源文件(自定义光标)
            android:textCursorDrawable="@drawable/edit_cursor_defult"
            android:textSize="14dp" />

光标资源文件:  edit_cursor_defult.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#21BC23" />
    <size android:width="1dp" />
</shape>

2:反编译修改EditText属性


public class CustomEditText extends EditText {
    public GeneralEditText(Context context) {
        super(context);
    }

    public CustomEditText (Context context, AttributeSet attrs) {
        super(context, attrs);
        modifyCursorDrawable(context, attrs);
    }

    public CustomEditText (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        modifyCursorDrawable(context, attrs);
    
    }


    private void modifyCursorDrawable(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
        int drawable = a.getResourceId(R.styleable.CustomEditText_textCursorDrawable, 0);
        if (drawable != 0) {
            try {
                Field setCursor = TextView.class.getDeclaredField("mCursorDrawableRes");
                setCursor.setAccessible(true);
                setCursor.set(this, drawable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     a.recycle();
    }
}

总结

简单总结了一下项目中兼容问题出现的EditText光标出现的问题特此记录一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值