2016-11-07 105 views
0

我有一個基本程序。我希望根據用戶點擊次數設置各個按鈕的背景,然後如果所有按鈕具有相同的背景,用戶將繼續下一個活動。很簡單,或者我想......我似乎無法弄清楚如何比較IF語句和其他語句。我花了一個小時寫作和重寫代碼,試圖找到答案。必須有一個簡單的答案,我錯過了,只是無法包圍我的思緒。我曾考慮實現我的目標如何將If語句組合成一個更大的If語句在Java中

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)) 

編輯線的東西**所以在嘗試了幾件事情後,我編輯我的代碼位和斜面仍然得到它的工作。還有什麼建議?

+0

「如何將IF語句與彼此進行比較」。你什麼意思? –

+0

目前還不清楚你打算如何處理這個問題,但是一開始,也許有不同的方法可以幫助你明確你要去的地方。 – Makoto

+0

我想要一個if語句,說「如果按鈕A1的背景是X,按鈕A2的背景是X然後去行動」 – Oliver

回答

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 
    } 
} 
+0

這並沒有解決我所需要的,但它有點幫助。我把這個項目擱置了一下,然後回到調試我已經啓動的另一個項目。感謝您的輸入。 – Oliver

0

我建議,在class裏面聲明clickCount變量,不要在裏面點擊處理程序。因此,您可以通過每個clickHandler方法實現每個按鈕的點擊計數。因此,編寫一個比較clickCount的方法,並在每個clickHandler中調用此方法。

private int clickCount1, clickCount2, clickCount3, clickCount4; 
private void compareClickCounts(){ 
    // Compare clickCounts and change buttons properties 
}