2014-07-04 82 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

相關問題