2011-05-16 60 views
0

如何在第二次單擊後使用「開關語句」將按鈕1的顏色切換爲按鈕2? 這些都是我的2個按鈕的點擊第二個按鈕單擊後的顏色開關

private int lCount = 0; 
private int rCount = 0; 
private int myCount = lCount & rCount; 

final TextView countTextViewPlusL = (TextView) findViewById(R.id.TextViewCountL); 
final Button countButtonPlusL = (Button) findViewById(R.id.ButtonCountPlusL); 

countButtonPlusL.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     myCount++; 
      if(myCount%2 == 0){ 
      countTextViewPlusL.setBackgroundColor(0xffffffff);} 
      else countTextViewPlusL.setBackgroundColor(0x00000000); 
     lCount++; 
     if (lCount >-1) 
     countTextViewPlusL.setText("" + lCount); 
    } 
}); 


final TextView countTextViewPlusR = (TextView) findViewById(R.id.TextViewCountR); 
final Button countButtonPlusR = (Button) findViewById(R.id.ButtonCountPlusR); 

countButtonPlusR.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     myCount++; 
      if(myCount%2 == 0){ 
      countTextViewPlusR.setBackgroundColor(0xffffffff);} 
      else countTextViewPlusR.setBackgroundColor(0x00000000); 
     rCount++; 
     if (rCount >-1) 
     countTextViewPlusR.setText("" + rCount); 
    } 
}); 
+0

'&'是一個按位運算符。你想添加lCount和rCount嗎?您應該使用'lCount + rCount'來代替。 – kcoppock 2011-05-16 20:33:22

回答

1

你是在增加你的聽衆lCount和/或rCount,但你奇偶測試myCount。這就是事情不會改變的原因。

+0

這是私有int: 'private int lCount = 0; private int rCount = 0; private int myCount = lCount&rCount;' **如何使用switch語句** – HunterX86 2011-05-16 20:27:46

+0

定義'mycount = lCount&rCount'會將'myCount'設置爲**不會更改的值**僅僅因爲'lCount'和/或'rCount'變化。我建議你寫一個函數'int myCount(){return lCount&rCount; }'並用它來代替。或者只是針對'((lCount&rCount)%2 == 0)'進行測試。 – 2011-05-16 20:34:44

+0

好吧,但現在我想要使用switch語句。我怎麼做? – HunterX86 2011-05-16 20:45:58

0

爲什麼你不嘗試這樣的事情呢?

private int lCount = 0; 
private int rCount = 0; 
private int myCount = 0; 

final TextView countTextViewPlusL = (TextView) findViewById(R.id.TextViewCountL); 
final Button countButtonPlusL = (Button) findViewById(R.id.ButtonCountPlusL); 
final TextView countTextViewPlusR = (TextView) findViewById(R.id.TextViewCountR); 
final Button countButtonPlusR = (Button) findViewById(R.id.ButtonCountPlusR); 

View.OnClickListener listener = new View.OnClickListener() { 
    public void onClick(View v) { 
     switch(v.getId()) { 
      case R.id.ButtonCountPlusR: 
       rCount++; 
       break; 
      case R.id.ButtonCountPlusL: 
       lCount++; 
       break; 
     } 
     myCount = lCount + rCount; 
     if(myCount % 2 == 0) { 
      //invert colors here 
     } 
    } 
}); 

countButtonPlusL.setOnClickListener(listener); 
countButtonPlusR.setOnClickListener(listener); 

初始化所有櫃檯到零,然後檢查每個點擊哪個按鈕被點擊,增加它的計數器,設置mycount的等於左側和右側櫃檯的總和,然後檢查如果是第二次點擊。在該檢查內部,反轉TextView顏色。

+0

我在最後一行出現錯誤。 「;」 - 此行有多個標記 \t - 語法錯誤,請插入「}」以完成塊 \t - 語法錯誤,插入「;」完成字段聲明 \t - 語法錯誤,插入「}」以完成ClassBody' 。 和我得到最後一個「}」語法錯誤的令牌「}」,{expected' – HunterX86 2011-05-16 21:56:20

+0

我剛剛離開了一些括號和分號。現在就試試。 – kcoppock 2011-05-16 22:05:21

+0

我在這裏必須填寫什麼內容://在此處反轉顏色。 以及如何 – HunterX86 2011-05-16 22:19:46

相關問題