如何更改EditText
框上的焦点颜色(橙色)?
焦点颜色是整个控件周围的一个小边缘,当控件具有焦点时为亮橙色。如何将该焦点的颜色更改为不同的颜色?
如何更改EditText
框上的焦点颜色(橙色)?
焦点颜色是整个控件周围的一个小边缘,当控件具有焦点时为亮橙色。如何将该焦点的颜色更改为不同的颜色?
您必须创建/修改自己的 NinePatch 图像以替换默认图像,并将其用作 EditText 的背景。如果你查看你的 SDK 文件夹,在你的平台下,然后是 res/drawable,你应该找到 EditText 焦点状态的 NinePatch 图像。如果这就是您想要更改的全部内容,您可以将其拉入 Photoshop 或您拥有的任何图像编辑软件,然后将橙色更改为您选择的颜色。然后将其保存到您的可绘制文件夹中,并构建一个新的 StateListDrawable,例如如下所示:
编辑text_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 可绘制对象。代码可能更简单。kotlin 中的示例:
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
// colorLine, colorLineFocus is vars of ColorStateList
ViewCompat.setBackgroundTintList(editText, if (hasFocus) colorLineFocus else colorLine)
}
<?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>