2013-02-07 39 views
0

我做了一個應用程序,其中我有18個按鈕(bAnswer1至bAnswer18)和6個文字瀏覽(tvAnswer1至6)。我的意思是用2個3字(如按鈕,bu - tt - on)分割6個單詞,我用6個單詞做了這些單詞,每個單詞放在一個切片中(切片1到18),所以這個代碼會重複自己的情況R. id.bAnswer18:你們會注意到它會是休息!我知道我可以通過添加一個for循環來使它非常簡單,但它不會工作,我不能離開?!Android - 如何把這個循環

有人能幫我擺脫這個嗎?

我的代碼:

public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

    switch (arg0.getId()) { 
    case R.id.bAnswer1: 

     if (testing1 != 6) { 
      String old = tvAnswer1.getText().toString(); 
      tvAnswer1.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 

     } 
     if (testing2 != 6) { 
      String old = tvAnswer2.getText().toString(); 
      tvAnswer2.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 
     } 
     if (testing3 != 6) { 
      String old = tvAnswer3.getText().toString(); 
      tvAnswer3.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 
     } 
     if (testing4 != 6) { 
      String old = tvAnswer4.getText().toString(); 
      tvAnswer4.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 
     } 
     if (testing5 != 6) { 
      String old = tvAnswer5.getText().toString(); 
      tvAnswer5.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 
     } 
     if (testing6 != 6) { 
      String old = tvAnswer6.getText().toString(); 
      tvAnswer6.setText(old + slice1); 
      bAnswer1.setVisibility(View.GONE); 
      break; 
     } 

     break; 
    case R.id.bAnswer2: 
     if (testing1 != 6) { 
      String old = tvAnswer1.getText().toString(); 
      tvAnswer1.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 

     } 
     if (testing2 != 6) { 
      String old = tvAnswer2.getText().toString(); 
      tvAnswer2.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 
     } 
     if (testing3 != 6) { 
      String old = tvAnswer3.getText().toString(); 
      tvAnswer3.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 
     } 
     if (testing4 != 6) { 
      String old = tvAnswer4.getText().toString(); 
      tvAnswer4.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 
     } 
     if (testing5 != 6) { 
      String old = tvAnswer5.getText().toString(); 
      tvAnswer5.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 
     } 
     if (testing6 != 6) { 
      String old = tvAnswer6.getText().toString(); 
      tvAnswer6.setText(old + slice2); 
      bAnswer2.setVisibility(View.GONE); 
      break; 
     } 
     break; 
+1

只需在它周圍放置一個for循環。正如你所說,你已經知道如何簡化它,所以簡單一點。 – Shark

+0

你應該有一個按鈕數組和另一個textviews數組,而不是6或18個字段。其餘的將是微不足道的。 –

+0

爲什麼你需要在這裏? – TheWhiteRabbit

回答

0

您正在尋找真正的答案是不是真的在一個for循環。

for循環會幫助,如果你做到這一點

OnClickListener myOnClick = new OnClickListener() { 
//privately paste what you had 
public void onClick(View v) { 
if (testing1 != 6) { 
     String old = tvAnswer1.getText().toString(); 
     tvAnswer1.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 

    } 
    if (testing2 != 6) { 
     String old = tvAnswer2.getText().toString(); 
     tvAnswer2.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 
    } 
    if (testing3 != 6) { 
     String old = tvAnswer3.getText().toString(); 
     tvAnswer3.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 
    } 
    if (testing4 != 6) { 
     String old = tvAnswer4.getText().toString(); 
     tvAnswer4.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 
    } 
    if (testing5 != 6) { 
     String old = tvAnswer5.getText().toString(); 
     tvAnswer5.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 
    } 
    if (testing6 != 6) { 
     String old = tvAnswer6.getText().toString(); 
     tvAnswer6.setText(old + slice1); 
     v.setVisibility(View.GONE); 
     break; 
    }}); 


Button[] answerButtons = new Button[18]; 

for(Button answer : answerButtons) 
    answer.setOnClickListener(myOnClick); 

記住,不要與你的活動實現OnClickListener,讓自己的意見處理它,除非你真的需要處理/調度onClicks自己。

+0

謝謝您的時間!但即時通訊在Android場景中並不遙遠,我看到的是你正在製作一個onclickListener,並且你把所有的18按鈕放在那裏,所以我不必再使用這些情況了? – Sevo

+0

另一件事是使用'break;'爲什麼不使用'else if'?如果這些都是你的情況,你可以移出v.setVisibility(View.GONE);來自if子句。 –

+0

Y我已經看到了一個哈哈,但仍然停留在相同的問題 – Sevo