2013-05-13 6 views
2

이 내 코드getCheckedradiobutton는 항상 -1을 반환

final RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1); 
RadioButton rb1 = (RadioButton) findViewById(R.id.radio0); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radio1); 
RadioButton rb3 = (RadioButton) findViewById(R.id.radio2); 
final CheckBox cb1 = (CheckBox) findViewById(R.id.chkbox1); 
int id = rg1.getCheckedRadioButtonId(); 
System.out.println("------------------------|"+id); 
switch (id) { 
    case R.id.radio0: 
     cb1.setEnabled(true); 
     break; 

    case R.id.radio1: 
     cb1.setEnabled(true); 
     break; 

    case R.id.radio2: 
     cb1.setEnabled(true); 
     break; 

    default: 
     cb1.setEnabled(false); 
     break; 
    } 

이 항상 -1을 반환 (chkbox 항상 사용 안 함)이며, 나는 그것이 작동되도록하는 것 기운 다. 플러스 ive 또한 시도하고 라디오 버튼 setId를 통해 개별 값을 할당하고 그것도 작동하지 않습니다.

내 XML이 작동하지 않습니다 물론

<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="46dp" > 

    <RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton1" 
     android:checkedButton ="1"   
     /> 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton2" 
     android:checkedButton="2"    
     /> 

    <RadioButton 
     android:id="@+id/radio2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton3" 
     android:checkedButton="3"    
     /> 
</RadioGroup> 
+0

로 변경이있는 곳입니다 getCheckedRadioButtonId()? – Sam

+0

체크 박스 선언 뒤'int id = rg1.getCheckedRadioButtonId()' – ShadowKnight

답변

0

입니다. 부동산 잘못된 뷰 : 당신은 당신은 "checkedButton을 안드로이드"를 사용하는이

rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 

     System.out.println("------------------------|"+checkedId); 
     switch (checkedId) { 
      ... 
     } 
    } 
}); 
+0

완벽하게 작동합니다. 고마워요 @Neoh – ShadowKnight

1

처럼 확인하는 라디오 버튼을 감지하는 리스너를 사용해야합니다. 이 속성은 각 라디오 버튼에이 속성을 추가하는 동안 그룹이 선택된 라디오 버튼을 식별합니다. 다음을 시도해보십시오

<RadioGroup 
android:id="@+id/radioGroup1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="46dp" 
android:checkedButton="2"    
> 

<RadioButton 
    android:id="@+id/radio0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="RadioButton1" 
    /> 

<RadioButton 
    android:id="@+id/radio1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="RadioButton2" 
    /> 

<RadioButton 
    android:id="@+id/radio2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="RadioButton3" 
    /> 

+0

나는 그것도 시도했지만 여전히 나던 일을했습니다. – ShadowKnight

관련 문제