Java设置edittext光标_Android 通过 xml 和 Java 两种方式调整 EditText 光标样式

小菜有个小需求是根据主题配色更改 EditText 中输入框光标的颜色,网上查了一些资料,大部分都是直接用的 xml 方式在做调整,但是小菜需要的是在 Java 代码中动态调整光标颜色。

虽然是一个很简单的东西,但是小菜在测试中还是遇到了不少的小问题,现在简单整理一下,希望对于遇到相同问题的朋友有所帮助。

小菜的测试步骤如下:

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

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" />

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

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" />

android:shape="rectangle">

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

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

}

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

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

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

}

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

添加一个测试 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);

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

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

小菜公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值