Android different EditText backgrounds for different states using shapes, selector or list

亡梦爱人 提交于 2019-11-27 05:16:56

I suggest to use NinePatch images to customize your EditText. Here goes an example based on my code:

The Selector:

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

Use selector the same way you used in your code, set it to background of your EditText.

Images:

twitter_im_edittext_focused.9.png

twitter_im_edittext_normal.9.png

More about NinePatch images you can found here.

Hope it helps.

Short version of accepted answer

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_focused="true"
        android:drawable="@drawable/edit_text_background_focused" />
    <item android:drawable="@drawable/edit_text_background_normal" />
</selector>

I think the problem is that your cursor is above the left 'border / wall' of the editText box and is not visible due to the overlap.

What I did was to add some padding for the text and this moved the cursor and text more 'inward' and it then became visible.

Try adding this to your editText:

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