如何将语句一起绑定到Java中的一个更大的If语句中

时间:2016-11-07 06:49:35

标签: java android

我有一个基本程序。我希望根据用户点击次数设置各个按钮的背景,然后如果所有按钮具有相同的背景,则用户将继续进行下一个活动。很简单,或者我认为......我似乎无法弄清楚如何将IF语句与其他语句进行比较。我花了一个小时编写和重写代码,试图找到答案。必须有一个简单的答案,我不知道,只是无法理解。

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }

    Button a1;
    Button a2;
    Button a3;
    Button a4;

    static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;


    public void addListenerOnButton() {



        final Button a1 = (Button) findViewById(R.id.a1);
        a1.setOnClickListener(new OnClickListener()                
                @Override
                public void onClick(View v) {
                    count1=count1+1;
                    if(count1==1||count1==4||count1==8)
                    {   a1.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked !", Toast.LENGTH_LONG).show();}
                    else if(count1==2||count1==6)
                    {a1.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();}
                    else{a1.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();
                    }}});
        final Button a2 = (Button) findViewById(R.id.a2);
        a2.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==2||clickcount==4||clickcount==7)
                    {   a2.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==1||clickcount==3)
                    {a2.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a2.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                    }}});
        final Button a3 = (Button) findViewById(R.id.a3);
        a3.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==1||clickcount==2||clickcount==6)
                    {   a3.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==4||clickcount==5||clickcount==7)
                    {a3.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a3.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                    }}});

        final Button a4 = (Button) findViewById(R.id.a4);
        a4.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==3||clickcount==5||clickcount==7||clickcount==8)
                    {   a4.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==2||clickcount==9)
                    {a4.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a4.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                }}});           



        }


}
        public void checkFunc(){
        if(count1==1)       

            {   
                Intent browserIntent =
                    new Intent(Intent.ACTION_VIEW,     Uri.parse("https://www.google.com/"));
                startActivity(browserIntent);

        }
    }


    }

我考虑实现目标的一种方式是

if((a1.setBackgroundColor(Color.BLUE)==true)||    (a2.setBackgroundColor(Color.BLUE)==true))

编辑**所以在尝试了一些事情后,我编辑了一些代码但仍无法使其工作。还有其他建议吗?

2 个答案:

答案 0 :(得分:1)

您可以使用静态变量进行比较。还要全局声明变量。这不是一个好方法,但这里有:

//global static vars
static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;

//make sure you include your conditions. I've added this just for reference
public void addListenerOnButton() {  
  a1.setOnClickListener(new OnClickListener() {  
@Override public void onClick(View v)  
  {
  count1++;
  }
}

a2.setOnClickListener(new OnClickListener() {  
  @Override public void onClick(View v)  
 {
  count2++;
  }
}

//And so on...
}   

 public void checkFunc()
{
if(count1==count2==count3==count4)
  {
  //do something
  }
}

答案 1 :(得分:0)

我建议,在class中声明clickCount变量,而不是在click处理程序中声明。因此,您可以通过每个clickHandler方法达到每个按钮的点击次数。因此,编写一个比较clickCount的方法,并在每个clickHandler中调用此方法。

Object.apply