EditText光标问题

问题描述 :
EditText hint内容的字体size大小和实际内容字体size大小不一样,所以采用了这种方式:

 val ss = SpannableString("可提现金额${StringUtils.getPrice(viewModel.balance)}元")//定义hint的值
 val ass = AbsoluteSizeSpan(14, true)//设置字体大小 true表示单位是sp
 ss.setSpan(ass, 0, ss.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
 etPrice.hint = SpannedString(ss)

利用spannable设置。

但是在小米手机上有问题,EditText光标错位,偏上。

解决办法:

自定义cursor

android:textCursorDrawable="@drawable/cursor"

cursor.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="2dp" />
    <solid android:color="#222222" />
    <padding
        android:bottom="@dimen/y60"
        android:top="0sp" />
</shape>

用padding字段,android:bottom="@dimen/y60"具体数值根据自己的实际情况。

cursor就算过长也不会显示出来,因为整个EditText的高度是一定的不会超出EditText的高度。

    原文作者:javalong
    原文地址: https://www.jianshu.com/p/c92299664933
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞