«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

[위미르 개발팀] Android, iOS , Web 제작

[안드로이드 레이아웃] 체크박스 직접 체크했는지 여부 받아오기 본문

개발자료/Android

[안드로이드 레이아웃] 체크박스 직접 체크했는지 여부 받아오기

위미르개발팀 2017. 12. 15. 09:27

체크박스 여러개를 사용할때 다음과같이 전체를 체크하는 체크박스가 있고, 이에 연동되는 다른 체크박스들이 있을때


전체를 누르면 나머지것들이 체크되고 전체를 제외하고 나머지중에 하나라도 체크해제하면 전체동의 부분만 체크해제 되어야 할때가 있습니다.


이를 전체동의를 누르면 모든 체크박스가 체크되고 나머지 3개가 모두 체크되면 전체동의 체크박스도 같이 체크되며

전체 체크된상태에서 하나라도 체크해제한다면 전체동의 체크박스가 같이 체크해제되도록 한 소스코드입니다.


@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.all_check:
isall = b;
if(b){
if(!second.isChecked())
second.setChecked(b);
if(!third.isChecked())
third.setChecked(b);
if(!fourth.isChecked())
fourth.setChecked(b);
}else{
if(all.isPressed()){ //전체동의 체크박스를 직접 체크 했는지?
second.setChecked(b);
third.setChecked(b);
fourth.setChecked(b);
}
}
break;
case R.id.second:
second= b;
if(second && third && fourth){
all.setChecked(true);
}else{
all.setChecked(false);
}
break;
case R.id.third:
third= b;
if(second && third && fourth){
all.setChecked(true);
}else{
all.setChecked(false);
}
break;
case R.id.fourth:
fourth= b;
if(second && third && fourth){
all.setChecked(true);
}else{
all.setChecked(false);
}
break;
}
}


setCheck를 할경우 onCheckedChanged가 돌기때문에 직접 체크를 했는지 알아야 의도대로 처리 할수 있습니다.


onCheckedChanged에서 isPressed()의 반환값이 직접 체크박스를 누른것인지에 대한 여부를 알고 처리 할수 있습니다.


만약 isPressed() 없이 한다면 모두 체크된 상태에서 second, third, fourth중 하나라도 체크를 해제한다만 all이 setChecked(false) 가 되면서 onCheckedChanged가 돌아서 모든 체크박스가 같이 해제되는 문제가 있습니다.










희 위미르에서는 모바일 어플리케이션(Android/iOS), Web 개발을 해드리고 있습니다.


문의 전화 : 070-4177-3962


Comments