按钮背景条件

时间:2014-01-04 21:08:54

标签: android background conditional-statements

如果按钮设置了背景并设置了另一个按钮,如何创建条件?

例如我想创建一个有两个条件的按钮点击

代码:

suono.setOnClickListener(new Button.OnClickListener() {  
               public void onClick(View v){

                   if(//has suonoff as background  ){
                       Button suonoff = (Button)findViewById(R.id.bsoundoff);
                       suonoff.setBackgroundResource(R.drawable.suonon);

                     }


                   Button suonoff = (Button)findViewById(R.id.bsoundoff);
                   suonoff.setBackgroundResource(R.drawable.suonoff);

2 个答案:

答案 0 :(得分:1)

维持一个标志是否单击,更改标志的状态。

boolean flag=true;
suonoff.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(flag){
                        suonoff.setBackgroundResource(R.drawable.suonon);
                        flag=false;
                    }else{
                        suonoff.setBackgroundResource(R.drawable.suonoff);
                        flag=true;
                    }
                }
            });

答案 1 :(得分:0)

你可以这样做:

Button suonoff = (Button)findViewById(R.id.bsoundoff);
if(suonoff.getBackground() == null){

    suonoff.setBackgroundResource(getResources().getDrawable(R.drawable.suonon));
}

Button suonoff = (Button)findViewById(R.id.bsoundoff);
if(suonoff.getBackground() == getResources().getDrawable(R.drawable.suonon)){

    suonoff.setBackgroundResource(getResources().getDrawable(R.drawable.suonoff));
}