2014-07-04 22 views
0

我在我的一项活动中有一个表格,并且有大约10个radiogroups。我的问题是,我必须检查一个radiogroups是否没有检查? 我的代码示例如下图所示: 检查一个活动中的所有radiogroups是否被检查

<RadioGroup 
     android:id="@+id/radioq1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/q1f" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="50dp" 
      android:text="@string/conffalse" 
      android:textColor="#f05252" /> 

     <RadioButton 
      android:id="@+id/q1t" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="60dp" 
      android:text="@string/conftrue" 
      android:textColor="#37df47" /> 

    </RadioGroup> 
+0

你有10个radiogroup或按钮? –

+1

您必须使用CheckBox而不是RadioButton/RadioGroup?作为您的要求,您必须检查多个项目。 –

+0

@PurpleDroid是的他有上面的代码10次 –

回答

0

您可以检查是否有任何按钮是一个radioGroup中的内部检查与:

int checkId = yourRadioGroup.getCheckedRadioButtonId(); 

boolean isChecked = false; 

if(checkedId==-1){ 

    isChecked=false; 
}else{ 

    isChecked=true; 
} 

与包含数组这样做对每一个RadioGroup中例如每RadioGroup中和一个for循环:

RadioGroup[] groups = new RadioGroup{RadioGroup1,RadioGroup2}; 

for(int i=0;i<groups.length;i++){ 

int id = groups[i].getCheckedRadioButtonId(); 
if(id==-1){ 

    isChecked=false; 
    break; //break the loop, because if one is not checked, inform the user 
}else{ 

    isChecked=true; 
    } 
} 

方法getCheckedRadioButtonId();如果没有选中radioButton,则返回-1。

+0

完美的解决方案,谢谢 – user3648435