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

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

和尚我的测试步骤如下:

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

26d6f8ee14677031a43192a433b36d05.png

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 设置光标颜色;

d18cac19a746ee323ac7f7771e404cea.png

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 方式调整光标颜色,但是设置失败;

9b3eb3a94b7e85ed7f83d314a2e158a1.png

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 方式调整即可;

3d53235b06fca324e652cddf72dba72f.png

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 属性即可;

3c67882fee24f2ccf4c6a4e088885e87.png

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 光标样式,会以最后一次设置的为准。

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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐