继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android 通过 xml 和 Java 两种方式调整 EditText 光标样式

老菜和尚
关注TA
已关注
手记 185
粉丝 165
获赞 165

小菜有个小需求是根据主题配色更改 EditText 中输入框光标的颜色,网上查了一些资料,大部分都是直接用的 xml 方式在做调整,但是小菜需要的是在 Java 代码中动态调整光标颜色。
      虽然是一个很简单的东西,但是小菜在测试中还是遇到了不少的小问题,现在简单整理一下,希望对于遇到相同问题的朋友有所帮助。


小菜的测试步骤如下:

  1. 设置一个默认的 EditText,默认光标颜色为程序对应的 colorPrimary 颜色值;

<EditText
    android:id="@+id/test_et1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="18dp"
    android:background="@null"
    android:hint="默认光标颜色,色值 #13B7F6" />

webp

  1. 设置一个 EditText,通过更改 xml 方式调整光标颜色,其中 android:textCursorDrawable 属性来设置 shape 光标样式,shape 中 size 设置光标宽度,solid 设置光标颜色;

<EditText
    android:id="@+id/test_et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="18dp"
    android:background="@null"
    android:hint="xml 设置光标颜色,色值 #F54343"
    android:textCursorDrawable="@drawable/editext_cursor" />
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="@color/red" /></shape>

webp

  1. 设置一个 EditText,期望通过 Java 方式调整光标颜色,但是设置失败;

<EditText
    android:id="@+id/test_et3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="18dp"
    android:background="@null"
    android:hint="Java 设置光标颜色(不正常)为灰色"
    android:textCursorDrawable="@null" />
GradientDrawable myGrad2 = new GradientDrawable();
myGrad2.setColor(getResources().getColor(R.color.cmbkb_limit_buy_green));
myGrad2.setSize(4, 40);try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);

    f.set(et3, myGrad2);
} catch (Exception ignored) {    // TODO: handle exception}


webp


Tips: 造成失败的原因有两个,第一个不可设置 android:textCursorDrawable="@null",这样光标颜色默认是根据字体颜色一致;第二个是不可以设置 new GradientDrawable(),并不能直接调整光标颜色。


  1. 设置一个 EditText,通过 Java 方式调整光标颜色,此效果为小菜期待的效果,将上个步骤中 Tips 方式调整即可;

<EditText
    android:id="@+id/test_et4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="18dp"
    android:background="@null"
    android:hint="Java 设置光标颜色(正常),色值 #00CC00"
    android:textCursorDrawable="@drawable/editext_cursor" />
GradientDrawable myGrad1 = (GradientDrawable) getResources().getDrawable(R.drawable.editext_cursor);
myGrad1.setColor(getResources().getColor(R.color.cmbkb_limit_buy_green));
myGrad1.setSize(4, 20);try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);

    f.set(et4, myGrad1);
} catch (Exception ignored) {    // TODO: handle exception}


webp


Tips: 小菜在测试过程中发现,需要在 EditText xml 中默认设置一个 android:textCursorDrawable="@drawable/editext_cursor" 样式,之后在 Java 代码动态修改光标颜色和宽度。


  1. 添加一个测试 EditText,Java 动态修改光标宽度,仅需调整 size 属性即可;

GradientDrawable myGrad2 = (GradientDrawable) getResources().getDrawable(R.drawable.editext_cursor);
myGrad2.setColor(getResources().getColor(R.color.cmbkb_limit_buy_green));
myGrad2.setSize(15, 40);

webp


Tips: 小菜在测试时发现,一旦用上述方式调整光标颜色,同一个页面中所有的 EditText 光标样式,会以最后一次设置的为准。


      很多看起来很细小的问题有时候也很值得研究,下面的是小菜的公众号,欢迎闲来吐槽哦~



作者:老菜和尚


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP