2011-10-23 52 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布局完成此操作。