Android 使用 selector 来改变TextView在不同状态下的textColor和background

        最近在项目中需要实现一个功能:可以点击TextView,当TextView被点击的时候,背景变成了红色,字体颜色变成了白色,当不点击的时候,字体是灰色的,背景是白色的。为了能够实现这个功能,我想到了selector,于是,在drawable文件夹下面定义两个文件,分别用于TextView的textColor 和 background:

      设置background :



    
    
      

     设置textColor:



    
    

selector 属性介绍:

android:state_selected选中

android:state_focused获得焦点

android:state_pressed点击

android:state_enabled设置是否响应事件,指所有事件


你可能感兴趣的:(安卓)