1、背景介绍

在做Android项目开发的时候,有时我们须要对button做一些特殊的处理,比方button点击的时候会有一个动画的效果,实际上就是几张图片在短时间的切换。再比方有时候我们须要对界面的选择的时候,须要表示该项目是选中状态还是不被选中状态。怎么办,相同使用两张图片。分别表示不同状态即可了。

2、代码演示样例

2.1 button的代码演示样例

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/menubar_home_pressed" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/menubar_home_pressed" />
<item android:state_enabled="true" android:drawable="@drawable/menubar_home_normal" />
<item android:state_focused="true" android:drawable="@drawable/menubar_home_pressed" />
</selector>

上面的这段代码,表示的是一个button的动画效果。

2.2 选中与失去焦点

</pre><pre name="code" class="html"><pre name="code" class="html"><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menubar_home_pressed" android:state_selected="true"/>
<item android:drawable="@drawable/menubar_home_normal"/>
</selector>

上面的代码中,item部分第一行表示选中状态。第二行表示默认状态。

3、问题解析

在2.2中的代码。我们假设写成下面形式:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menubar_home_normal"/>
<item android:drawable="@drawable/menubar_home_pressed" android:state_selected="true"/>
</selector>

假设2.2中的item位置交换一下。第一行放在第二行的以下。那么我们能够在xml文件里看到一条黄色的波浪线。例如以下图所看到的:

我们能够看到。代码中有这种提示:

This item is unreachable because a previous item (item #1) is a more general match than this one

这句话就是说第二行item之前。不能有元素出现。假设这样放置,会出现button一直是默认状态,而无法获得焦点的情况。

selector的button选中处理问题的更多相关文章

  1. jquery获取单选button选中的值

    在页面上单选button的代码: <s:iterator value="@com.hljw.cmeav.util.CmeavGlobal@isComMap"> < ...

  2. 微信小程序button选中改样式-实现单选/多选

    小程序实现多button单选/多选 红色为选中状态 单选 多选 ①wxss /* pages/button-select/button-select.wxss */ .button_container ...

  3. iOS Button选中与取消

    /** * 是否点击 */ @property (nonatomic ,assign) BOOL selected; /** *  button 点击事件选中取消选中 */ - (void)handl ...

  4. android selector设置button点击效果(具体)以及常见问题

    button的点击效果学习起来其实比較easy,此点对开发人员来说也是使用的比較频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到 ...

  5. <已解决>使用selector设置Button按下松开的样式以及 <item> tag requires a 'drawable' attribute or child tag defining a drawable 报错

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="ht ...

  6. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  7. android背景选择器selector用法汇总

    一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...

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

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

  9. Android的selector,背景选择器

    原文地址 http://android.blog.51cto.com/268543/564581 首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下 ...

随机推荐

  1. 紫书第三章训练1 E - DNA Consensus String

    DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...

  2. php单一入口和多入口模式详细讲解

    php单一入口模式可谓是现在一种比较流行的大型web应用开发模式,比如当前比较流行的一些php开发框架,zend,thinkphp,qeephp,还有cakephp 等他们都是采用的单一入口模式的.本 ...

  3. [LOJ#6002]「网络流 24 题」最小路径覆盖

    [LOJ#6002]「网络流 24 题」最小路径覆盖 试题描述 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交)的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是  ...

  4. Python Base One

    //this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...

  5. 升级springboot 2.x 踩过的坑——跨域导致session问题

    目前IT界主流前后端分离,但是在分离过程中一定会存在跨域的问题. 什么是跨域? 是指浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域. 做过web后台的童鞋都知道,跨 ...

  6. wamp出现You don’t have permission to access/on this server提示

    本地搭建wamp,输入http://127.0.0.1访问正常,当输入http://localhost/,apache出现You don't have permission to access/on ...

  7. 线段树懒标记好题 HDU4578

    (1)"1 x y c",代表 把区间 [x,y] 上的值全部加c (2)"2 x y c",代表 把区间 [x,y] 上的值全部乘以c (3)"3 ...

  8. <深入理解计算机系统> CSAPP Tiny web 服务器

    本文是我学习<深入理解计算机系统>中网络编程部分的学习笔记. 1. Web基础       web客户端和服务器之间的交互使用的是一个基于文本的应用级协议HTTP(超文本传输协议).一个w ...

  9. Django 博客首页视图

    Django 处理 HTTP 请求 Web 应用的交互过程其实就是 HTTP 请求与响应的过程.无论是在 PC 端还是移动端,我们通常使用浏览器来上网,上网流程大致来说是这样的: 我们打开浏览器,在地 ...

  10. php转换字符编码为utf-8

    php转换字符编码为utf-8 function strToUtf8($str){ $encode = mb_detect_encoding($str, array("ASCII" ...