2011-10-23 5 views
0

Comment regrouper tous les radioButtons dynamiques créés dans un groupe?Android TableLayout group dynamicButtons

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> 

Répondre

0

Pour traiter les boutons radio en tant que groupe, ils doivent être placés dans une mise en page RadioGroup. Si vous placez plutôt les boutons radio dans des cellules de tableau séparées, vous devrez implémenter vous-même le comportement du groupe.

Vous pouvez utiliser RadioButton.SetOnCheckedChangeListener() pour enregistrer un écouteur qui répond aux événements vérifiés.

Vous pouvez regarder le RadioGroup source code pour voir comment cela est fait avec la disposition RadioGroup.