开发者

Android EditText native selector

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 the native selector is working my edittext becomes smaller. Can anyone please suggest why this is happening?Here is my code snippet:

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

And here I use my selector:

 <EditText 
        android:layout_height="37dip" 
        android:layout_width="fill_parent"
        android:layout_marginLeft="7dip"
        android:layout_marginRight="7dip"
        android:background="@layout/selector_input"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:inputType="textEmailAddress">
    </EditText>

This is the code of my_border:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white"/>
    <stroke android:width="1dip" android:color="@color/border_green"/>
    <corners android:radius="4dp" /> 
    <padding android:left="1dip" android:top="1dip" android:right="1dip" android:bottom="1dip" />开发者_如何学Python
</shape>

P.S. absolutely the same thing is happening with the buttons.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜