我正在尝试切换按钮的背景drawable,以便当用户单击按钮时,其背景会发生变化,当用户再次单击该按钮时,其背景将返回默认值.这是我的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我很确定这不是你如何使用drawables与if语句.有人可以建议一种方法吗?