【发布时间】:2015-03-29 14:07:01
【问题描述】:
我正在尝试切换按钮的背景可绘制对象,以便当用户单击按钮时,其背景会发生变化,而当用户再次单击按钮时,其背景会恢复为默认值。这是我的代码:
public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);
if(star.getBackground().equals(R.drawable.btn_star_off)) {
star.setBackgroundResource(R.drawable.btn_star_on);
} else {
star.setBackgroundResource(R.drawable.btn_star_off);
}
}
我很确定这不是您在 if 语句中使用可绘制对象的方式。有人可以建议一种方法吗?
【问题讨论】:
标签: android button background toggle drawable