Android EditText native selector

筅森魡賤 提交于 2019-12-02 02:18:25

at first, thanks a lot for your post, helped me alot, but your code above (sample for selector) wasnt working for me:

so i adapted it for others, so if someone searches howto mark a textfield as selected:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/bottom_line_normal"/>
<item 
android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/bottom_line_normal"/>
<item 
android:state_pressed="true" 
android:drawable="@drawable/bottom_line_focus"/>
<item 
android:state_enabled="true" android:state_focused="true" 
android:drawable="@drawable/bottom_line_focus"/>
<item 
android:state_enabled="true" 
android:drawable="@drawable/bottom_line_normal"/>
<item 
android:state_focused="true" 
android:drawable="@drawable/bottom_line_focus"/>
<item 
android:drawable="@drawable/bottom_line_normal"/>
</selector>

have fun :=) good luck :)

Since no one was able to completely answer my question I will answer it by myself. The problem was that my edittext became smaller every time I touched it. That happens because native edittext background in Android has a transparent area around it. So I used a layer list to create my background also with a transparent area around it. Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/transparent_background"
        android:top="5dip"
        android:right="5dip"
        android:bottom="5dip"
        android:left="5dip" />
    <item
        android:drawable="@drawable/my_border"
        android:top="0dip"
        android:right="0dip"
        android:bottom="0dip"
        android:left="0dip" />
</layer-list>

In my selector instead of my_border I use this xml. That's it. Easy to do and hell difficult to find out how!

<item 
android:drawable="@drawablemy_border" /> 

1. are you missing a slash [/]? And what is the definition of that drawable my_border

Ok, after I tried this files, the EditText doesn't use your selector when it's not enabled and appear with it's standard layout... so adding

<item android:state_enabled="false" android:drawable="@android:drawable/edit_text"/> to your selector should be the solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!