2013-02-07 4 views
0

18 개의 버튼 (bAnswer1 ~ bAnswer18)과 6 개의 텍스트 뷰 (tvAnswer1 ~ 6)가있는 앱을 만들고 있어요. 내 뜻은 6 단어를 2 단어 3 개 (예 : 버튼, 연속)로 슬라이싱하는 것입니다.이 단어를 6 단어로 나누어 슬라이스 (슬라이스 1 ~ 18)에 넣으면이 코드는 R 경우에 반복됩니다. id.bAnswer18 : 그리고 너희들은 휴 (hugh)가 될 것이라는 것을 알아 차릴 것이다 !! 나는 for 루프를 추가함으로써 정말 간단하게 만들 수 있다는 것을 알고 있지만, 작동하지 않을 것이고 나올 수 없다.안드로이드 - 이것을 for 루프에 넣는 법

누군가 나를 도와주세요. ??

내 코드 :

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

6 개 또는 18 개의 필드 대신 단추 배열과 다른 textviews 배열이 있어야합니다. 나머지는 사소 할 것입니다. –

+0

여기 왜 필요합니까? – TheWhiteRabbit

답변

0

당신이 찾고있는 진짜 대답은에 대한 루프에 정말 없습니다.

에 대한 루프 당신이 정말로/파견 onClicks를 직접 처리 할 필요가없는 경우, 뷰 자체가 그것을 처리 할 수 ​​있도록, 당신이 당신의 활동에 OnClickListener를 구현하지 않는,이

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

이 기억하는 경우에 도움이 될 것입니다.

+0

감사합니다! 하지만 안드로이드 씬에서 멀지 않은 곳에서 볼 수 있듯이, onclickListener를 만들고 18 개의 버튼을 모두 넣으면 더 이상 사례를 사용하지 않아도됩니까? – Sevo

+0

다른 것은'break;를 사용하고'else if'를 사용하지 않는 이유는 무엇입니까? 그 모든 경우 귀하는 v.setVisibility (View.GONE)로 이동할 수 있습니다. if 절에서. –

+0

Y 나는 이미 그 하나의 하하를 보았지만 같은 질문에 여전히 붙어있다. – Sevo