android Selector的使用

强大的selector的使用,通常我们总是在代码中进行点击按钮后在设置背景,操作起来挺麻烦的,其实有更简单的使用方法,就是selector;

布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <EditText 
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        />

    <Button
        android:id="@+id/bt"
        android:layout_centerInParent="true"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_bt"
        android:text="测试" />
    
    <Button
        android:layout_marginTop="20dp"
        android:layout_below="@+id/bt"
        android:id="@+id/bt2"
        android:layout_centerInParent="true"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_bt"(引用)
        android:text="测试2" />
    

</RelativeLayout>

drawable下的布局如下:

bg_bt.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- <item android:state_pressed="true" android:state_focused="false"  android:drawable="@color/red"/> -->
    <item android:state_selected="true" android:drawable="@color/red" /> //表示被选中的状态
    <item  android:drawable="@color/green"/>// 默认没有被选中的状态

</selector>

在代码中只需要一句话就可以搞定了

 

 1 @Override
 2     public void onClick(View v) {
 3         switch (v.getId()) {
 4         case R.id.bt:
 5             bt.setSelected(true);
 6             bt2.setSelected(false);
 7             Intent intent = new Intent(this, BTest.class);
 8             startActivity(intent);
 9             break;
10         case R.id.bt2:
11             bt.setSelected(false);
12             bt2.setSelected(true);//这句话是关键,表示点击按钮点击了被选中的状态
13             break;
14 
15         default:
16             break;
17         }
18     }

效果图:

   默认的图片:

    

点击之后的效果图:

 

其实这种使用的方法很灵活结合控件的属性如下:

 bt.setPressed(pressed);

 bt.setEnabled(enabled);

 bt.setFocusable(focusable);

 以上的都可以实现结合selector中的样式使用一句代码就可以修改页面中控件的样式;

 

  并附上selector中不同属性的说说明:

 

可以设置以下几种触发状态:

 

英文的说明:

android:state_pressed

Boolean . "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.

如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

 

android:state_focused

Boolean . "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.

true,获得焦点时显示;false,没获得焦点显示默认。

 

android:state_selected

Boolean . "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.

true,当被选择时显示该图片;false,当未被选择时显示该图片。

 

android:state_checkable

Boolean . "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

 

android:state_checked

Boolean . "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.

true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

 

android:state_enabled

Boolean . "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.

true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。

 

android:state_window_focused

Boolean . "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。

 

纯中文的解释:

android:state_pressed           如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

android:state_focused           如果是true,获得焦点时显示;如果是false没获得焦点显示默认。

android:state_selected          如果是true,当被选择时显示该图片;是false未被选择时显示该图片。

android:state_checkable         如果值为true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

android:state_checked           如果值为true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

android:state_enabled           如果值为true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
 
android:state_window_focused    如果值为true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。

 

和selector的集合使用还有很多,后续会陆续的发表上来:

 

 

  

 

转载于:https://www.cnblogs.com/kingfly13/p/3868664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值