2011-01-12 15 views
11

Código ... {Radio Control Dinámico Botón

private void createRadioButton() { 

     final RadioButton[] rb = new RadioButton[5]; 
     for(int i=0; i<5; i++){ 
      rb[i] = new RadioButton(this); 
      ll.addView(rb[i]); 
      rb[i].setText("Test"); 
     } 
     ll.addView(submit); 
      submit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       for(int i = 0; i < 5; i++) { 
        ll.removeView(rb[i]); 
       } 
       ll.removeView(submit); 
       Questions(); 
     }}); 
    } 

El problema que estoy teniendo es que los botones de radio aparecen y el usuario puede seleccionar cualquiera. Como principiante, estoy seguro de que no estoy configurando los botones de radio correctamente. El usuario puede seleccionar los cinco botones y luego, una vez seleccionados, tampoco pueden desmarcarlos. El usuario solo debería poder seleccionar una opción de las cinco ... ¿cómo puedo hacer esto posible?

Respuesta

16

Debe agregar los botones de selección para una RadioGroup y luego el RadioGroup a la disposición

echo de menos algo de información como lo que es presente, pero su código debe ser similar:

private void createRadioButton() { 
    final RadioButton[] rb = new RadioButton[5]; 
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup 
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
    for(int i=0; i<5; i++){ 
     rb[i] = new RadioButton(this); 
     rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout 
     rb[i].setText("Test"); 
    } 
    ll.addView(rg);//you add the whole RadioGroup to the layout 
    ll.addView(submit); 
    submit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      for(int i = 0; i < 5; i++) { 
       rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup 
      } 
      ll.removeView(submit); 
      Questions(); 
     } 
    }); 
} 
+0

: cómo agregar la identificación? –

+1

@ShahzadImam ¿Qué tal un setId()? – spaaarky21

+0

@ spaaarky21: Lo resolvió hace un año .. gracias. –

4

Tienes que crear un RadioGroup en el archivo de diseño

<TableRow> 
    <RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/radiobuttons"> 
    </RadioGroup> 
</TableRow> 

y luego agregar mediante programación los botones a la misma:

public void makeRadioButtons(Vector tmpVector, int i, 
LinearLayout.LayoutParams lp) 
{ 
    RadioButton rb = new RadioButton(this); 
    rb.setText((String) tmpVector.elementAt(i)); 
    //rg is private member of class which refers to the radio group which you can find by id. 
    rg.addView(rb, 0, lp); 

} 

Espero que esto ayude.

+0

No uso xml porque necesito hacer estos botones dinámicamente ya que podría haber 1,2 o 15 botones creados por la aplicación. – Beginner

+0

No estoy seguro de cómo funcionan los botones o grupos de radio en este momento. ¿Agrego el botón al grupo y luego visualizo el grupo? – Beginner

+0

@Usmaan: Sí, primero agregue el botón para agrupar y luego mostrar todo el grupo. Para referencia: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/RadioGroup1.html –

3

Su diseño.

<LinearLayout 
    android:id="@+id/linearMain" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
    <RadioGroup 
     android:id="@+id/radiogroup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
    > 
    </RadioGroup> 
</LinearLayout> 

código

RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);//not this RadioGroup rg = new RadioGroup(this); 
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
    for(int i=0; i<5; i++) 
    { 
     rb[i] = new RadioButton(this); 
     rg.addView(rb[i]); 
     rb[i].setText("Test"); 
    } 

esperanza de que esto ayude.

Cuestiones relacionadas