一、需求背景:
项目开发中经常遇到输入框,有时候需要自定义光标
二、预期效果:
三、实现方式
1、xml格式
文件布局
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_bg"/>
drawable资源,用shape控制是一个长方形:
2、动态设置(适用于需要动态更改光标样式)
/**
* 反射设置光标颜色 R.drawable.edittext_cursor
*
* @param edittextView
* @param drawable 资源文件
*/
public static void setCursorColor(EditText edittextView, int drawable) {
try {//修改光标的颜色(反射)
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(edittextView, drawable);
} catch (Exception e) {
//Log.e(TAG,e);
}
}
四、特殊问题
1.魅族,小米