如何改变Android中EditText的焦点颜色

如何更改EditText框上的焦点颜色(橙色)? 焦点颜色是整个控件周围的一个小圈,当控件具有焦点时,它是亮橙色。 我如何将焦点的颜色改变为不同的颜色? 任何人都可以帮我解决这个问题? 提前致谢,

您必须创建/修改您自己的NinePatch图像来替换默认的图像,并将其用作EditText的背景。 如果您查看您的SDK文件夹,在您的平台下,res / drawable,您应该找到EditText焦点状态的NinePatch图像。 如果这就是所有你想改变的东西,你可以把它拉到Photoshop,或任何图像编辑软件,并将橙色更改为您选择的颜色。 然后将其保存到您的可绘制文件夹中,然后构建一个新的StateListDrawable,例如下面的内容:

edittext_modified_states.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@android:drawable/edittext_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/edittext_focused_blue" /> <!-- focused --> <item android:drawable="@android:drawable/edittext_normal" /> <!-- default --> </selector> 

我不知道EditText的默认NinePatches的实际名称,所以根据需要替换它们,但是这里的关键是只使用@android:drawable图像作为你没有修改的图像(或者你可以复制他们到您的项目的可绘制文件夹),然后使用您的修改绘图为您的重点状态。

然后,可以将此StateListDrawable设置为TextView的背景,如下所示:

 <TextView android:background="@drawable/edittext_modified_states" 
 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:color="colorcode" /> <!-- pressed --> <item android:state_focused="true" android:color="colorcode" /> <!-- focused --> <item android:color="colorcode" /> <!-- default --> </selector> 

你不需要创建xml drawables。 代码中可能更简单。 在kotlin中的例子:

 editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus -> // colorLine, colorLineFocus is vars of ColorStateList ViewCompat.setBackgroundTintList(editText, if (hasFocus) colorLineFocus else colorLine) }