Создание радиогрупп с более чем тремя кнопками радио?

StackOverflow https://stackoverflow.com//questions/22081607

  •  24-12-2019
  •  | 
  •  

Вопрос

Я пытаюсь сделать приложение с вопросами, и каждый вопрос имеет 4 варианта ответов (множественный выбор), и я использовал 4 радиопередачи для 4 ответов.Есть ли способ присоединиться к ним вместе в радиогруппе или я должен обрабатывать каждую переключатель индивидуально?Радиогруппа имеет всего 3 радиотена, могу ли я увеличить радиопередачу более трех?Если да, то как?

Это было полезно?

Решение

Вы можете получить каждый радиообуттон из кода, как:

RadioGroup rg = (RadioGroup )findViewById(R.id.radio_group);

RadioButton r1 = (RadioButton) rg.getChildAt(0);
RadioButton r2 = (RadioButton) rg.getChildAt(1);
RadioButton r3 = (RadioButton) rg.getChildAt(2);
RadioButton r4 = (RadioButton) rg.getChildAt(3);
.

Да, вы можете добавить больше, чем 3 переключателя в радиогрупп

<RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />
        </RadioGroup>
.

Другие советы

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

final RadioButton[] rb = new RadioButton[4];
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<4; 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); 
.

Редактировать:

Или вы можете просто определить радиогруппу в вашем XML:

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

А затем добавьте одну дополнительную кнопку на нее программически:

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top