我想将颜色更改回默认颜色。
如果我想点击按钮;按钮的颜色必须改变。如果我再次点击相同的按钮......颜色必须重新设置为默认值。
我怎样才能实现这一目标......
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = false;
}
});
答案 0 :(得分:2)
试试这种方式
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
clicked = false;
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
clicked = true;
}
}
});
或在Selector
中创建drawable/selector.xml
,如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/like_icon_hover" />
<item
android:state_pressed="false"
android:drawable="@drawable/like_icon" />
</selector>
并将此Selector
设置为Background
Button
,如:
android:background="@drawable/selector"
答案 1 :(得分:0)
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = !clicked;
}
});