Android EditText native selector

前端 未结 4 979
日久生厌
日久生厌 2021-01-23 03:51

I try to apply my own design to edittext and to use android native selector when my edittext is enabled, focused, etc. The problem is that every time I touch the edittext and th

相关标签:
4条回答
  • 2021-01-23 04:19

    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!

    0 讨论(0)
  • 2021-01-23 04:24

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

    0 讨论(0)
  • 2021-01-23 04:43

    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

    0 讨论(0)
  • 2021-01-23 04:45
    <item 
    android:drawable="@drawablemy_border" /> 
    

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

    0 讨论(0)
提交回复
热议问题