android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

首先看到selector的属性:
android:state_focused
android:state_pressed
android:state_selected
android:state_enabled

它们之间的执行是 有执行顺序的写xml的时候特别要分析好执行顺序,否则达不到要实现的效果:

现在实现效果如下:


当点击停止按钮时,




有点击效果,和选中效果。
具体代码如下:
暂停:


<?xml version="1.0" encoding="utf-8"?>

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

<item android:drawable="@drawable/icon_pause_active" android:state_pressed="true"/>

    <item android:drawable="@drawable/icon_pause_active" android:state_selected="true"/>

    <item android:drawable="@drawable/icon_pause_inactive" android:state_enabled="true"/>

</selector>


停止;
<?xml version="1.0" encoding="utf-8"?>

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

<item android:state_pressed="true" android:drawable="@drawable/icon_stop_active"/>

<item android:state_selected="true" android:drawable="@drawable/icon_stop_active"/>

<item android:state_enabled="true" android:drawable="@drawable/icon_stop_inactive"/>

播放:

</selector>


<?xml version="1.0" encoding="utf-8"?>

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

  <item android:state_pressed="true" android:drawable="@drawable/icon_play_active"/>

<item android:state_selected="true" android:drawable="@drawable/icon_play_active"/>

<item android:state_enabled="true" android:drawable="@drawable/icon_play_inactive"/>

</selector>


布局中:


 <ImageButton

                android:layout_marginRight="15dp"

                android:background="@null"

                android:id="@+id/stop"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:src="
@drawable/music_stop_style" />

            <ImageButton

                android:id="@+id/play"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:background="@null"

                android:layout_marginRight="15dp"

                android:layout_marginLeft="15dp"

                android:src="
@drawable/music_play_style" />

<ImageButton

                android:layout_marginLeft="15dp"

                android:id="@+id/pause"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:background="@null"

                android:src="@
drawable/music_pause_style" />



代码中:
public void onClick(View arg0) {


// TODO Auto-generated method stub


switch (arg0.getId()) {


case R.id.stop:


stop.setSelected(true);


play.setSelected(false);


pause.setSelected(false);


break;


case R.id.play:


stop.setSelected(false);


play.setSelected(true);


pause.setSelected(false);


break;


case R.id.pause:


stop.setSelected(false);


play.setSelected(false);


pause.setSelected(true);


break;


     处理好即可、。

android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现的更多相关文章

  1. Android——selector背景选择器的使用详解(二)

    在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML ...

  2. Android selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...

  3. Android中selector背景选择器

    http://blog.csdn.net/forsta/article/details/26148403 http://blog.csdn.net/wswqiang/article/details/6 ...

  4. Android:关于背景选择器Selector的item顺序

    在使用背景选择器的时候,如果item的顺序不对,会导致不起作用. 1.首先背景选择器的normal选项一定要放在最后. 2.pressed的选择器应该在seclet的前面.我在使用的时候找了半天问题, ...

  5. Android View 背景选择器编写技巧

    在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...

  6. android中的selector背景选择器的用法

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...

  7. Android的selector 背景选择器

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法.首先android的selector是在d ...

  8. [转]永久告别Android的背景选择器Selector!无需切很多图了!

    package com.zoke.custom.autobg; import android.content.Context; import android.content.res.TypedArra ...

  9. Android:res之selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中android:state_focused--------获得焦点a ...

随机推荐

  1. 如何将CELERY放到后台执行?

    在作正式环境,这个是必须的. 于是找了两小时文档, 以下这个方法,相对来说好实现. 就是要注意supervisord.conf的目录存放位置. 放在DJANGO的PROJ目录下,是最佳位置. http ...

  2. Android ListView(Selector 颜色)

    listview_color.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  3. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-004- 处理上传文件

    一.用 MultipartFile 1.在html中设置<form enctype="multipart/form-data">及<input type=&quo ...

  4. Markdown简单语法总结

    . 标题 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 2. 列表 无序列表 * 西瓜 * 葡萄 * 香蕉 - 西瓜 - 葡萄 - ...

  5. ruby使用IO类读写文件

    path="test.txt" port=open(path) begin port.each_line{|line| p line.to_s } ensure port.clos ...

  6. 【HDOJ】5046 Airport

    DLX简单题目. /* 5046 */ #include <iostream> #include <string> #include <map> #include ...

  7. WPF——文本随滚动条改变而改变

    一.造一个窗体,拖进一个文本框TextBox和滚动条Slider 二.让文本框的内容随滚动条的滚动而改变,即文本框绑定到滚动条上 三.实现效果

  8. ☀【移动】UC极速模式

    UC浏览器的部分版本默认是“极速”模式,有何办法能控制UC自动改变其浏览模式? √http://www.zhihu.com/question/20582949 关于UC极速模式下访问网站错乱 √htt ...

  9. Matlab read_grib.r4 安装新方法(转自:http://blog.sina.com.cn/s/blog_9f36648b010179s7.html)

    最近,打算用Matlab处理些GRIB格式的文件,需要在Matlab中添加read_grib.m文件,其实相当于是添加一个工具箱.read_grib.m的下载见:http://www.renci.or ...

  10. 步步为营 SharePoint 开发学习笔记系列总结

    转:http://www.cnblogs.com/springyangwc/archive/2011/08/03/2126763.html 概要 为时20多天的sharepoint开发学习笔记系列终于 ...