2014-02-27 24 views
0

我試圖提出一個問題的應用程序,每個問題有4個答案選項(多項選擇),我用4個單選按鈕來解答4個答案。有沒有辦法將它們連接在一個radiogroup中,或者我應該單獨處理每個單選按鈕?一個收音機組只有3個單選按鈕,我可以將單選按鈕增加到三個以上嗎?如果是,那麼如何?用三個以上的單選按鈕創建一個radiogroups?

+0

非常感謝烏拉圭回合的幫助 – user3225760

回答

0

你可以從代碼中獲得每個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); 

是的,你可以添加更多的則三個單選按鈕在無線電集團

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

是否足夠來定義XML中的RadioGroup中的單選按鈕無碼? – user3225760

+0

您將其定義爲我寫的,然後在您的活動的onCreate中使用 RadioGroup rg =(RadioGroup)findViewById(R.id.radioGroup1);然後,你可以從這個radiogroup中獲得每個單選按鈕,如下所示: RadioButton r1 =(RadioButton)rg.getChildAt(0); RadioButton r2 =(RadioButton)rg.getChildAt(1); RadioButton r3 =(RadioButton)rg.getChildAt(2); RadioButton r4 =(RadioButton)rg.getChildAt(3); 之後,你可以使用每個單選按鈕。 – Dadroid

0

您必須將單選按鈕添加到RadioGroup,然後將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定義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"); 
    } 
相關問題