我正在尝试切换按钮的背景可绘图形,这样当用户单击按钮时,其背景将更改,当用户再次单击按钮时,其背景将返回到默认值。以下是我的代码:
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
语句的正确方式。请有人给出一种方法吗?