android中如何更改EditText 的光标颜色?

android中如何更改EditText 的光标颜色?,第1张

EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色

android:textCursorDrawable="@null","@null"作用是让光标颜色和text

color一样

1 在代码中进行设置更改默认提示的字体大小

SpannableString hintStr = new SpannableString(getResources().getString(R.string.search_tittle))// 定义hint的值

AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14, true)// 设置字体大小 true表示单位是SP

hintStr.setSpan(ass, 0, hintStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

search_et.setHint(new SpannedString(hintStr))

2 在布局文件中进行设置EditText更改光标的的宽度和颜色

android:textCursorDrawable="@drawable/shape_et_cursor"

shape资源是

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<size android:width="2dp" />

<solid android:color="#FF5899"  />

</shape>

3 EditText常用属性

android:textCursorDrawable="@null" 控制光标颜色 "@null"不设置颜色和editText的颜色一致 设置颜色要使用

@drawable/shape 属性

editText.setSelection(2) 使光标移动到制定的位置

editText.requestFocus() 请求出现光标时获取焦点

editText.clearFocus()      清除光标,失去焦点

editText.setCursorVisible(false) 不出现光标

android:imeOptions="actionSearch" d出键盘出现搜索按钮

android:inputType="text" 输入字符串

如图:

/res/drawable 文件夹下创建 edit_cursor_color.xml :

布局文件中设置 EditText 的 textCursorDrawable 属性为 edit_cursor_color :


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/6370933.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-21
下一篇2023-03-21

发表评论

登录后才能评论

评论列表(0条)

    保存