按钮onclick不改变背景图像

时间:2012-11-22 11:05:55

标签: android image button onclick drawable

在按钮上单击我想更改按钮的背景图像,但它不适用于我。

确切的问题是什么?

总是感谢帮助。

      private Button seatdirnbtn;
      seatdirnbtn.setOnClickListener(seathtlistner);
 private View.OnClickListener seathtlistner = new View.OnClickListener() {
 public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.drawable.icon4:
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
        break;
    case R.drawable.icon4hlt:
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);

    }
}

1 个答案:

答案 0 :(得分:1)

尝试在用户点击按钮时更改按钮背景:

 private Button seatdirnbtn;
 public static boolean isclick=false;
 seatdirnbtn.setOnClickListener(seathtlistner);
 private View.OnClickListener seathtlistner = new View.OnClickListener() {
 public void onClick(View v) {
    // TODO Auto-generated method stub
   if(isclick){
      seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }else{
      seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    }
    isclick=!isclick;
}