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

发布于 2024-10-10 10:21:04 字数 111 浏览 3 评论 0原文

如何更改 EditText 框中的焦点颜色(橙色)?

焦点颜色是整个控件周围的小边框,并且是明亮的 当控件获得焦点时为橙色。我怎样才能改变它的颜色 聚焦到不同的颜色?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-10-17 10:21:04

您必须创建/修改自己的 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"

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:

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.

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

<TextView
    android:background="@drawable/edittext_modified_states"
夏日落 2024-10-17 10:21:04

您不需要创建 xml 可绘制对象。代码上可能更简单。
科特林中的示例:

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

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

editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
    // colorLine, colorLineFocus is vars of ColorStateList
    ViewCompat.setBackgroundTintList(editText, if (hasFocus) colorLineFocus else colorLine)
}
我要还你自由 2024-10-17 10:21:04
<?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 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文