简体   繁体   中英

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. How do?

You have to define the drawables for each state of your edit text. You create an xml file in your drawables folder that defines all the state drawables for your edit text. The XML file looks like this:

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

<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. Then all you have to do is define in your styles.xml

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

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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