1.利用drawable XML
在res/drawable下创建文件,如:myselect.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/buttom_focused"
/>
<item android:state_checked="true"
android:drawable="@drawable/buttom_click"/>
<item android:drawable="@drawable/buttom_normal"/>
</selector>
<ImageButton
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sure"
android:src="@drawable/myselect"
android:onClick="isClicked"/>
如果要关注它的点击事件,在main.xml对于的Activity中可以这样调用:
public void isClicked(View view){
//todo
}
用这种方法要特别注意item 的顺序,有点像if/esleif的关系,就是说一旦第一个item触发了就不会判断下一个了。
2.在代码中改变背景图片
boolean notice_btn_flag = true;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fun_set_notice_btn:
if(notice_btn_flag){
Resources resources = getApplicationContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.notice_btn_b);
notice_btn.setBackgroundDrawable(btnDrawable);
notice_btn_flag = false;
}else {
Resources resources = getApplicationContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.notice_btn_a);
notice_btn.setBackgroundDrawable(btnDrawable);
notice_btn_flag = true;
}
}
}