Android开发————Button点击改变颜色

转自:http://blog.csdn.net/howlaa/article/details/18311101

需求是在我按下按钮时,该变按钮颜色,使用户感觉到自己按了按钮,当松开的时候,变回原来的颜色。

正常时:


按下时:


有人说,直接监听按钮的按下事件不得了嘛,其实这样确实能实现同样的效果,但是有个缺点,比如很多按钮都需要这样的效果,那你同样的代码就要重复很多次。所以,还是要通用起来。

首先,在res文件夹下新建一个文件夹drawable,这是无关分辨率的:

在下面建立一个xml文件:login_button_selector.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.         <item android:drawable="@drawable/clr_normal" android:state_pressed="false"/>  
  4.         <item android:drawable="@drawable/clr_pressed" android:state_pressed="true"/>  
  5.   
  6.     </selector>  

然后在value文件夹下的string.xml文件里添加:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <drawable name="clr_normal">#ff6501</drawable>  
  2.     <drawable name="clr_pressed">#a44100</drawable>  

最后为button添加:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <Button  
  2.             android:layout_marginTop="15dp"  
  3.             android:layout_width="match_parent"   
  4.             android:layout_height="wrap_content"  
  5.             android:text="@string/loginSubmit"  
  6.             android:id="@+id/login"  
  7.             android:textColor="@android:color/white"  
  8.             android:background="@drawable/login_button_selector"  
  9.             />  


OK。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值