如何在 Android 中更改 EditText 的焦点颜色 [英] How to change focus color of EditText in Android

查看:82
本文介绍了如何在 Android 中更改 EditText 的焦点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改 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?

推荐答案

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

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>

我不知道 EditText 的默认 NinePatches 的实际名称,因此根据需要替换它们,但这里的关键是仅使用 @android:drawable 图像作为这些图像您尚未修改(或者您可以将它们复制到项目的 drawable 文件夹中),然后将修改后的 drawable 用于您的聚焦状态.

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.

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

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

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

这篇关于如何在 Android 中更改 EditText 的焦点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆