如何在Android中更改EditText的焦点颜色

时间:2022-11-21 15:09:46

How can I change the focus color (orange) on an EditText box? The focus color is a small rim around the entire control and is bright orange when the control has focus. How can I change the color of that focus to a different color? Can anyone help me in sorting out this issue? Thanks in advance,

如何在EditText框中更改焦点颜色(橙色)?焦点颜色是整个控件周围的小边缘,当控件具有焦点时亮橙色。如何将焦点的颜色更改为其他颜色?任何人都可以帮我解决这个问题吗?提前致谢,

3 个解决方案

#1


62  

You'll have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder, and build a new StateListDrawable, for example something like the below:

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

edittext_modified_states.xml

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>

I don't know offhand the actual names for the default NinePatches for the EditText, so replace those as necessary, but the key here is to just use the @android:drawable images for the ones you haven't modified (or you can copy them over to your project's drawable folder), and then use your modified drawable for your focused state.

我不知道EditText的默认NinePatches的实际名称,所以必要时替换它们,但这里的关键是只使用@android:drawable图像来处理你没有修改过的图像(或者你可以复制)将它们转移到项目的可绘制文件夹中),然后使用修改过的drawable来处理您的聚焦状态。

You can then set this StateListDrawable as the background for your TextView, like so:

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

<TextView
    android:background="@drawable/edittext_modified_states"

#2


0  

<?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>

#3


0  

You dont need to create xml drawables. It may be more simple in code. Example in kotlin:

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

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

#1


62  

You'll have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder, and build a new StateListDrawable, for example something like the below:

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

edittext_modified_states.xml

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>

I don't know offhand the actual names for the default NinePatches for the EditText, so replace those as necessary, but the key here is to just use the @android:drawable images for the ones you haven't modified (or you can copy them over to your project's drawable folder), and then use your modified drawable for your focused state.

我不知道EditText的默认NinePatches的实际名称,所以必要时替换它们,但这里的关键是只使用@android:drawable图像来处理你没有修改过的图像(或者你可以复制)将它们转移到项目的可绘制文件夹中),然后使用修改过的drawable来处理您的聚焦状态。

You can then set this StateListDrawable as the background for your TextView, like so:

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

<TextView
    android:background="@drawable/edittext_modified_states"

#2


0  

<?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>

#3


0  

You dont need to create xml drawables. It may be more simple in code. Example in kotlin:

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

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