如何在没有代码更改的情况下更改EditText背景,只有xml?

时间:2018-04-05 09:22:32

标签: android android-edittext

我有很多EditText视图,现在我需要更改背景颜色。但是将侦听器设置为每个视图都会产生大量代码(like here)。有没有办法写一个选择器或类似的东西?我试着写一个简单的选择器,它有一个活动/非活动状态,但它不起作用。 这个选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/LightGray" />
    <item android:state_enabled="true"  android:color="@color/Blue" />
    <item android:color="@color/LightGray" />
</selector>

4 个答案:

答案 0 :(得分:0)

要使用xml更改EditText背景颜色,您必须创建一个定义背景的drawable。示例代码:

<EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        />

bg.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector 
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <item 
      android:state_pressed="true"
      android:color="your_color" 
    /> 
    <item 
       android:state_focused="true"
       android:color="your_color"
    /> 
    <item 
           android:color="your_color"
    /> 
</selector>

答案 1 :(得分:0)

  

检查并尝试:

当您输入内容或edittext将具有焦点时,文字颜色将为红色,否则为黑色

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

答案 2 :(得分:0)

你可以试试这个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true"  android:color="@color/Blue" />
   <item android:color="@color/LightGray" />
</selector>

答案 3 :(得分:0)

edit_background,.xml

<selector xmlns:android=”http://schemas.android.com/apk/res/android”>

    <item android:drawable=”@drawable/edittext_normal” 
android:state_enabled=”true” android:state_window_focused=”false”/>
    <item android:drawable=”@drawable/edittext_normal” 

android:state_enabled=”false” android:state_window_focused=”false”/>
    <item android:drawable=”@drawable/edittext_press” 

android:state_enabled=”true” android:state_focused=”true”/>
    <item android:drawable=”@drawable/edittext_normal” 

android:state_enabled=”true”/>
    <item android:drawable=”@drawable/edittext_press” 

android:state_focused=”true”/>
    <item android:drawable=”@drawable/edittext_normal”/>

    </selector>

edittext_normal.vml

 <?xml version=”1.0″ encoding=”utf-8″?>
<layer-list xmlns:android=”http://schemas.android.com/apk/res/android” >

<!– “background shadow” –>
<item>
<shape android:shape=”rectangle” >
<solid android:color=”#ffffffff” />

<stroke
android:width=”1dp”
android:color=”#e3e3e3″ />
</shape>
</item>

</layer-list>

edittext_press.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<layer-list xmlns:android=”http://schemas.android.com/apk/res/android” >

<item>
<shape android:shape=”rectangle” >
<solid android:color=”@color/progress_blue” />
</shape>
</item>
<item
android:bottom=”0dp”
android:left=”0dp”
android:right=”0dp”
android:top=”0dp”>
<shape android:shape=”rectangle” >
<solid android:color=”@color/progress_blue” />
</shape>
</item>
<item
android:bottom=”2dp”
android:left=”2dp”
android:right=”2dp”
android:top=”2dp”>
<shape android:shape=”rectangle” >
<solid android:color=”@color/textwhite” />
</shape>
</item>

</layer-list>

和你的Edittext

<EditText
android:id=”@+id/edttext”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@drawable/editbackground”
android:textSize=”15dp” />