Подтвердить что ты не робот

Управление динамической радиокнопкой

Код... {

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();
         }});   
    }

Проблема, с которой я столкнулся, заключается в том, что появляются переключатели, и пользователь может выбрать любой из них. Являясь новичком, я уверен, что я неправильно настраиваю радио кнопки. Пользователь может выбрать все пять кнопок, а затем один раз выбрать, они также не могут их снять. Пользователь должен только выбрать один из пяти вариантов... как я могу сделать это возможным?

4b9b3361

Ответ 1

Вы должны добавить радиокнопки RadioGroup, а затем RadioGroup в макет

Я пропущу некоторую информацию, как то, что подает, но ваш код должен выглядеть так:

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();
        }
    });   
}

Ответ 2

Вам нужно создать RadioGroup в файле макета

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

а затем программно добавить к нему кнопки:

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);

}

Надеюсь, что это поможет.

Ответ 3

Ваш макет.

    <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>

код

   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");
    }

надеюсь, что это поможет вам.