首先定义一个获得StateListDrawable对象的方法:

private StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) {
StateListDrawable sd = new StateListDrawable();
Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
sd.addState(new int[]{android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
sd.addState(new int[]{android.R.attr.state_enabled}, normal);
sd.addState(new int[]{}, normal);
return sd;
}

其中,就如注释中所讲的addState的顺序相当重要。

使用ddStateDrawable

//……前面对Button的声明略去
okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));

    // Bitmap转换成Drawable
public Drawable bitmap2Drawable(Bitmap bitmap) {
BitmapDrawable bd = new BitmapDrawable(getResources(), bitmap);
Drawable d = (Drawable) bd;
return d;
} private StateListDrawable addStateDrawable(Bitmap idNormal, Bitmap idPressed, Bitmap idFocused) {
StateListDrawable sd = new StateListDrawable();
Drawable normal = bitmap2Drawable(idNormal);
Drawable pressed = bitmap2Drawable(idPressed);
Drawable focus = bitmap2Drawable(idFocused);
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
sd.addState(new int[]{android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
sd.addState(new int[]{android.R.attr.state_enabled}, normal);
sd.addState(new int[]{}, normal);
return sd;
}
   

java代码中实现android背景选择的selector-StateListDrawable的应用的更多相关文章

  1. 【转】java代码中实现android背景选择的selector-StateListDrawable的应用

    原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1712.html 下面的代码应该很多人都熟悉: 1 2 3 ...

  2. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  3. Android color(颜色) 在XML文件和java代码中

    Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...

  4. IDEA插件:快速删除Java代码中的注释

    背景   有时,我们需要删除Java源代码中的注释.目前有不少方法,比如: 实现状态机.该方式较为通用,适用于多种语言(取决于状态机支持的注释符号). 正则匹配.该方式容易误判,尤其是容易误删字符串. ...

  5. UML中类关系表示与Java代码中的对应关系

    UML中类关系表示与Java代码中的对应关系 1. 类的UML表示法 上图中,Employee 类有两个String类型的私有属性和一个返回值为String类型public 方法 getName(); ...

  6. paip.java 开发中web server的选择jboss resin tomcat比较..

    paip.java 开发中web server的选择jboss resin tomcat比较.. 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专 ...

  7. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  8. 使用mongo-java-driver3.0.2.jar和mongodb3.0在java代码中的用户验证4

    以下是使用mongo-java-driver3.0.2.jar和mongodb3.0.4在java代码中的用户验证: ServerAddress sa = new ServerAddress(host ...

  9. 关于在Java代码中写Sql语句需要注意的问题

    最近做程序,时不时需要自己去手动将sql语句直接写入到Java代码中,写入sql语句时,需要注意几个小问题. 先看我之前写的几句简单的sql语句,自以为没有问题,但是编译直接报错. String st ...

随机推荐

  1. 如何在IntelliJ IDEA中快速配置Tomcat

    近来使用idea编写java代码的人越来越多,最关键的就是idea强大的代码提示功能,能极高的提升程序员的开发效率,但是毕竟各有所长,idea中tomcat的配置就没有eclipse那么轻松,这里简单 ...

  2. Android学习之——GridView

    背景知识 GridView在Android开发中和ListView一样经常被使用.如我们经常使用的快图浏览,里面就有将图片的布局改为网格(即GridView)的选项.还有约X神器——陌陌的搜索界也是用 ...

  3. localstorage和sessionstorage上手使用记录

    通过阅读各路大神对web存储locastorage和sessionstorage的用法解析,自己试用了一下,在此留个备忘. 在项目中,如果用到很多次storage,要存储很多数据,就要把它封装成函数了 ...

  4. redis 的set数据类型

    相关命令 1.SADD SADD key-name item1 [item 2…] 将一个或多个成员元素加入到集合中 2.SREM SMEMBERS  key-name item1 [item 2…] ...

  5. 【D】分布式系统的CAP理论

    2000年7月,加州大学伯克利分校的Eric Brewer教授在ACM PODC会议上提出CAP猜想.2年后,麻省理工学院的Seth Gilbert和Nancy Lynch从理论上证明了CAP.之后, ...

  6. 马尔科夫链蒙特卡洛(Markov chain Monte Carlo)

    (学习这部分内容大约需要1.3小时) 摘要 马尔科夫链蒙特卡洛(Markov chain Monte Carlo, MCMC) 是一类近似采样算法. 它通过一条拥有稳态分布 \(p\) 的马尔科夫链对 ...

  7. Java利用while循环计算1+1/2!+1/3!……+1/20!

    编写程序,用while语句计算1+1/2!+1/3!……+1/20!,并在控制泰山输出计算结果.要求1+1/2!+1/3!……+1/20!,其实就是求1+1*1/2+1*1/2*1/3+……+1*1/ ...

  8. windows防火墙设置端口开放技巧

    选择“打开或者关闭windows防火墙”把防火墙打开,然后选择“高级设置”,选择“创建规则”来指定端口.(这里也可以在“入站规则”里选择已经存在的端口.) 指定ip开放3389端口 某新服务器,开放8 ...

  9. Linux中的邮件发送

    这里写出两种常用的邮件发送方式: mail: 需要安装sendmail和postfix两个服务 编辑/etc/mail.rc,在最后添加 set from=scottcho@126.com smtp= ...

  10. oracle nvl,having的用法

    select oi.order_id,opl.payment_no,opl.back_no, oi.commit_time, oi.receive_mobile, oi.receive_user, n ...