2013-05-13 98 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 2013-05-13 05:49:46

+0

复选框声明后'int id = rg1.getCheckedRadioButtonId()' – ShadowKnight 2013-05-13 05:51:26

回答

0

当然它不会工作。您应该使用监听器来检测该单选按钮被选中,这样

rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

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

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

完美工作。谢谢@ Neoh – ShadowKnight 2013-05-13 06:16:54

1

您使用的是“机器人:checkedButton”财产上的错误观点。此属性标识哪个单选按钮是一个组被选中,而您将此属性添加到每个单选按钮。请尝试更改以下

<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 2013-05-13 05:56:40