Android:检查后台资源Drawable

时间:2013-05-04 14:20:43

标签: android background resources drawable

请帮助我们如何进行背景资源检查?

示例:

  Button button1;

  protected void onCreate(Bundle savedInstanceState) {
  ..........................................
  button1 = (Button) findViewById(R.id.example1);
  }

  public void onclick1 (View v){
         button1.setBackgroundResource(R.drawable.example2);
  }

  public void onclick2 (View v){

我的问题在这里,检查button1按钮是否为drawable = example2

        if (..........................){
               //Action
        }

如果没有,点击后会做另一个动作

        else {
               //Another Action
        }
   }

1 个答案:

答案 0 :(得分:4)

您可以对两个按钮使用View类的getBackground()方法,并将其比较如下:

if (button1.getBackground().getConstantState().equals(button2.getBackground().getConstantState())) {
    }
else {
    }
相关问题