3 개 이상의 라디오 버튼이있는 RadioGrougs를 만드는 것은 무엇입니까?
-
24-12-2019 - |
문제
나는 질문을 적용하려고하고 있으며 각 질문에는 4 개의 답변 옵션 (다중 선택)을 가지고 있으며 4 개의 답변을 위해 4 개의 라디오 버튼을 사용했습니다.변호적 그룹에 함께 가입하거나 각 라디오 버튼을 개별적으로 처리 해야하는 방법이 있습니까?라디오 그룹에는 3 개의 라디오 버튼이 있습니다. 라디오 버튼을 3 개 이상 늘릴 수 있습니까?그렇다면 어떻게됩니까?
해결책
다음과 같은 코드에서 각 RadioButton을 가져올 수 있습니다.
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>
. 다른 팁
라디오 버튼을 RadioROUP 및 RADIOGOROUG로 추가해야합니다.
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에서 radioGroup을 정의 할 수 있습니다.
<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");
}
. 제휴하지 않습니다 StackOverflow