Android StateListDrawable的使用

    在Android应用的开发过程中,我们经常要根据控件的状态,改变控件的显示,比如EditeText在focus,或者是unfocus时设置背景颜色的不同,这个相信大家在以前肯定看到过,通过Selector就可以实现,但是如何让EditeText在focus,或者是unfocus时设置不同的颜色呢?接下来说的ColorStateList就是这样的功能。

     新建一个项目,为了便于观察,在默认的main.xml文件里面拖入两个EditeText,然后在drawable文件夹中新建一个color_stat_list.xml,在里面输入以下内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="#FF0000" android:state_focused="true" ></item>
    <item android:color="#00FF00" android:state_focused="false" ></item>	
</selector>

编辑EditText,设置它们的android:textColor="@drawable/color_state_list",形如:

<EditText
        android:id="@+id/editText1"
        android:textColor="@drawable/color_state_list"
        android:text="@string/app_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
	
    </EditText>

       <EditText
        android:id="@+id/editText1"
        android:textColor="@drawable/color_state_list"
        android:text="@string/app_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
	
    </EditText>

保存项目,然后运行。效果如下图所示:

转载于:https://my.oschina.net/fengcunhan/blog/52637

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值