简体   繁体   English

Android EditText Style.xml更改FOCUS EditText

[英]Android EditText Style.xml change FOCUS EditText

Good afternoon, 下午好,

Guys I did a style file for all fields style.xml EditText now when the field gaining focus, I want to change the style. 伙计我为所有领域做了一个样式文件style.xml EditText现在当该领域获得焦点时,我想改变风格。 How do? 怎么办?

You have to define the drawables for each state of your edit text. 您必须为编辑文本的每个状态定义drawable。 You create an xml file in your drawables folder that defines all the state drawables for your edit text. 您在drawables文件夹中创建一个xml文件,该文件定义编辑文本的所有状态drawable。 The XML file looks like this: XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

You make one item for each state drawable you want to specify. 您为要指定的每个可绘制状态创建一个项目。 So for a drawable for state_focused you'd just do: 所以对于state_focused的drawable,你只需:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/myFocusedEditText"
        android:state_focused="true"/>
    <item
        android:drawable="@drawable/myEnabledEditText"
        android:state_enabled="true"/>
</selector>

Name this XML file, custom_edit_text.xml, make sure it's in the drawables folder. 将此XML文件命名为custom_edit_text.xml,确保它位于drawables文件夹中。 Then all you have to do is define in your styles.xml 然后你要做的就是在styles.xml中定义

<style name="my_edit_text">
    <item name="android:drawable">@drawable/custom_edit_text</item>
</style>

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM