解决国产自定义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光标出现的问题特此记录一下