android小知识之button按下是切换图片

有时候我我们需要对按键的背景进行改变,尤其是在被按下的时候,松开时又可以回复到原来的背景,提高用户体验。

首先需要写一个xml文件,设置button 的背景资源文件,实例代码如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item android:state_pressed="true" android:drawable="@drawable/btn_login_qq_pressed" ></item>
5     <item android:drawable="@drawable/btn_login_qq_normal"></item>
6 
7     </selector>
btn_welcome_regist.xml

然后在布局文件中的button添加android:background="@drawable/btn_welcome_regist"属性即可,按键按下时显示btn_login_qq_pressed背景,松开时显示

btn_login_qq_normal背景。

同理,改变<item android:state_focused="true">表示获得焦点时等等。

posted @ 2013-12-03 09:12  寡蛋  阅读(392)  评论(0编辑  收藏  举报