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的集合使用还有很多,后续会陆续的发表上来:

android Selector的使用

时间: 2024-08-09 06:21:42

android Selector的使用的相关文章

android selector(如对TextView点击样式改变)

selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that represents the registration. The keys are also add

Android Selector 与 Shape 基本用法

分类: Android2011-07-19 11:07 7513人阅读 评论(4) 收藏 举报 androidencodingbutton测试c 1:Selector drawable的item中可以有以下属性: android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focuse

Android UI设计系统-android selector 开始自定义样式

Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed="true/false"

Android -- selector&amp;&amp;StateListDrawable

selector <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 触摸时并且当前窗口处于交互状态 --> <item android:state_pressed="true" android:state_w

Android selector选择器的使用

通常按钮在点击前和后有两种状态,比如点击前为蓝色,点击后为灰色,且不再响应点击事件. 如果不使用selector选择器,点击后,就需要在程序中进行以下的类似操作 button1.setBackgroundResource(R.color.material_grey_300); button1.setTextColor(getResources().getColor(R.color.material_grey_50)); button1.setClickable(false); 如果使用selec

Android selector item 属性大全(按钮按下不同效果)

<selector>         必须.必须是根元素.包含一个或多个<item>元素.          Attributes:             xmlns:android                   String,必须.定义XML的命名空间,必须是                    “http://schemas.android.com/apk/res/android”.    <item> android:state_pressed Bool

android selector(转)

Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed="true/false"

android selector设置button点击效果(详细)以及常见问题

button的点击效果学习起来事实上比较容易,此点对开发者来说也是使用的比较频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到的drawable的seletor. 当然drawable中还有很多其他效果可以实现,具体的可以参考笔者的另一篇博客: android修改控件外观(使用drawable资源) 效果:(不点击时显示白色,点击时显示灰色) 实现这个效果其实很简单,在drawable中创建一个xml文件,然后输入两行代码即可

Android Selector和Shape的用法

一.Shape的用法 :shape用于设定形状,可以在selector,layout等里面使用,有6个子标签,各属性如下: 填充:设置填充的颜色 间隔:设置四个方向上的间隔 大小:设置大小 圆角:同时设置五个属性,则Radius属性无效 android:Radius="20dp"          设置四个角的半径 android:topLeftRadius="20dp"              设置左上角的半径 android:topRightRadius=&q