2011-10-23 56 views
0

如何將所有動態創建的收音機按鈕分組到一個組中?Android TableLayout組動態收音機按鈕

while (cursor.moveToNext()) { 
    TableRow row = new TableRow(this); 
    // CheckBox chk = new CheckBox(this); 
     RadioButton radioBtn = new RadioButton(this); 
    // chk.setText(cursor.getString(cursor.getColumnIndex("from_text"))); 
     radioBtn.setText(cursor.getString(cursor.getColumnIndex("from_text"))); 
     radioBtn. 
     row.addView(radioBtn); 
     table.addView(row); 
} 

的Xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

     <TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:id="@+id/myTable"/>   
</LinearLayout> 

回答

0

爲了處理單選按鈕,因爲它們具有一組被放置到一個RadioGroup佈局。如果您要將單選按鈕放在單獨的表格單元格中,那麼您必須自己實現組行爲。

您可以使用RadioButton.SetOnCheckedChangeListener()註冊響應選中事件的偵聽器。

您可以查看RadioGroup source code,瞭解如何使用RadioGroup佈局完成此操作。