2014-02-16 5 views
0
//Priority checkbox  
q1 = (CheckBox)findViewById(R.id.q1a); 

//"Sub" checkboxes 
q2 = (CheckBox)findViewById(R.id.q2a); 
q3 = (CheckBox)findViewById(R.id.q3a); 
q4 = (CheckBox)findViewById(R.id.q4a); 
q5 = (CheckBox)findViewById(R.id.q5a); 


if q1 is checked and 2 or more of the "sub" checkboxes are checked then{ 
new AlertDialog.Builder(this).setMessage(R.string.positive).show(); 
} 

누군가이 Eclipse에서 루프를 사용하여이 구문을 코딩하는 데 도움을 줄 수 있습니까?이 구문을 사용하여 루프를 사용하는 방법은 무엇입니까?

답변

1

이러한 체크 박스를 배열/목록에 저장해야 루프를 통해 반복 할 수 있습니다. 또한

q1 = (CheckBox)findViewById(R.id.q1a);  

ArrayList<CheckBox> cbList = new ArrayList<CheckBox>(); 
cbList.add((CheckBox)findViewById(R.id.q2a)); 
cbList.add((CheckBox)findViewById(R.id.q3a)); 
cbList.add((CheckBox)findViewById(R.id.q4a)); 
cbList.add((CheckBox)findViewById(R.id.q5a)); 

if (q1.isChecked()){ 
    int count = 0; 
    for (CheckBox cb : cbList) { 
     if (cb.isChecked()) { 
      count ++; 
     } 
    } 
    if (count >= 2){ 
     new AlertDialog.Builder(this).setMessage(R.string.positive).show(); 
    } 
} 

setMessage(R.string.positive) 당신이 단지 R에서 생성 된 번호로 메시지를 설정합니다으로 달성하기 위해 노력하고있다 (그리고 아마도 당신에 당신을 기다리고 오류를 줄 것이다 무엇 아마이므로주의 : 여기에 가능한 솔루션입니다 String를 입력).

new AlertDialog.Builder(this).setMessage(getString(R.string.positive)).show(); 

편집 : 더 최적화가 시도되지 않았는지

for (CheckBox cb : cbList) { 
    if (cb.isChecked()) { 
     count ++; 
    } 
} 

if (q1.isChecked() || count >= 2){ 
    new AlertDialog.Builder(this).setMessage(R.string.positive).show(); 
} 

참고 : 같은 될 것이라고 주석에 요청 대체 당신은 사실로 그 라인을 가질 것이다.

+0

좋아요. 그 중 하나를 시도해 보겠습니다. – user3085149

+0

내가 거기에 esle을 넣을 수 있습니다. q1 또는 하위 체크 박스 (1 또는 2 이상) 중 하나만 선택하면 결과가 setMessage (R.string.negative)입니다. @Vlad – user3085149

+0

최근 게시물을 편집하기 위해 게시물을 편집했습니다. 문제. 2 조건 중 하나라도 참인 경우 유효성을 검사하기를 원하면 확실하지 않습니다. 둘 다 맞거나 유일합니다. –

0

둘 이상의 확인란이 선택되어 있으면 전역 변수를 true로 설정하는 별도의 함수를 만듭니다.

각 확인란의 이벤트 핸들러를 사용하십시오.

관련 문제