前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 通过 xml 和 Java 两种方式调整 EditText 光标样式

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

作者头像
阿策小和尚
发布2019-08-12 14:49:41
3.3K0
发布2019-08-12 14:49:41
举报
文章被收录于专栏:阿策小和尚阿策小和尚

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


和尚我的测试步骤如下:

  1. 设置一个默认的 EditText,默认光标颜色为程序对应的 colorPrimary 颜色值;
代码语言:javascript
复制
<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" />
  1. 设置一个 EditText,通过更改 xml 方式调整光标颜色,其中 android:textCursorDrawable 属性来设置 shape 光标样式,shape 中 size 设置光标宽度,solid 设置光标颜色;
代码语言:javascript
复制
<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" />
代码语言:javascript
复制
<?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>
  1. 设置一个 EditText,期望通过 Java 方式调整光标颜色,但是设置失败;
代码语言:javascript
复制
<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" />
代码语言:javascript
复制
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(),并不能直接调整光标颜色。

  1. 设置一个 EditText,通过 Java 方式调整光标颜色,此效果为和尚我期待的效果,将上个步骤中 Tips 方式调整即可;
代码语言:javascript
复制
<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" />
代码语言:javascript
复制
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 代码动态修改光标颜色和宽度。

  1. 添加一个测试 EditText,Java 动态修改光标宽度,仅需调整 size 属性即可;
代码语言:javascript
复制
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 光标样式,会以最后一次设置的为准。


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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 阿策小和尚 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档