小编And*_*mes的帖子

以编程方式更改按钮背景可绘制onClick

我正在尝试切换按钮的背景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语句.有人可以建议一种方法吗?

android background button toggle drawable

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

background ×1

button ×1

drawable ×1

toggle ×1