2012-04-04 7 views
1

방사선 그룹에 3 개의 라디오 버튼이 있습니다. 어떤 단추가 선택되었는지에 따라 Java에 다른 작업을 수행하도록 지시하려면 어떻게합니까? 나는 그룹이 있고 모든 버튼 선언 :어떤 radiogroup 버튼을 선택했는지 확인하는 방법은 무엇입니까?

final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize); 
     final RadioButton small = (RadioButton)findViewById(R.id.RBS); 
     final RadioButton medium = (RadioButton)findViewById(R.id.RBM); 
     final RadioButton large = (RadioButton)findViewById(R.id.RBL); 

는 나는이 같은 말을 알고 :

if (size.getCheckedRadioButtonId().equals(small){ 

} else{ 

} 

을하지만 올바른 구문되지 않습니다 일치 한 ... 어떻게 자바를 요청할 수있는 버튼이 선택 되었습니까?

+0

이 http://www.thetekblog.com/2010/07/android-radiobutton-in-radiogroup에서 참조하시기 바랍니다 : 당신은 (R.id.RBS입니다) small의 ID와 getCheckedRadioButtonId()을 비교해야 -예/ –

답변

1

시도 :

if (size.getCheckedRadioButtonId() == small.getId()){ 
.... 
} 
else if(size.getCheckedRadioButtonId() == medium.getId()){ 
.... 
} 
1

getCheckedRadioButtonId() 때문에 반환 정수, 당신은 RadioButton 구성 객체 정수를 비교하기 위해 노력하고 있습니다.

switch(size.getCheckedRadioButtonId()){ 
    case R.id.RBS: //your code goes here.. 
        break; 
    case R.id.RBM: //your code goes here.. 
        break; 
    case R.id.RBL: //your code goes here.. 
        break; 
} 
1
int selected = size.getCheckedRadioButtonId(); 

switch(selected){ 
case R.id.RBS: 
    break; 
case R.id.RBM: 
    break; 
case R.id.RBL: 
    break; 

} 
관련 문제