2017-07-28 60 views
0

你好,我想創建一個radiogroup 2x2,所以我實現了這個使用兩個radiogroup。但現在的問題是我可以選擇兩個單選按鈕,而不是隻有一個。我該如何嘗試做到這一點?如何創建一個radiogroup 2x2?

我精確地使用kotlin來開發。

謝謝!

+0

沒有這樣的事情。 RadioGroup繼承自LinearLayout,因此可以是** vertical **或** horizo​​ntal。 –

+0

我想創建一個2x2 Radiobuttons!我認爲它顯然存在! –

+0

**不在單個RadioGroup中,顯然!**您可以自由設置2 * 2 RadioButton的網格。但是,你不會有RadioGroup給出的「相互排斥」。 –

回答

0

這裏你有一個非常類似的問題gridlayout 3x3 因爲你不能想要你想要微不足道的,你應該接受上述問題的答案之一。我所做的:

public class GRadioGroup { 

    List<RadioButton> radios = new ArrayList<RadioButton>(); 

    /** 
    * Constructor, which allows you to pass number of RadioButton instances, 
    * making a group. 
    * 
    * @param radios 
    *   One RadioButton or more. 
    */ 
    public GRadioGroup(RadioButton... radios) { 
     super(); 

     for (RadioButton rb : radios) { 
      this.radios.add(rb); 
      rb.setOnClickListener(onClick); 
     } 
    } 

    /** 
    * Constructor, which allows you to pass number of RadioButtons 
    * represented by resource IDs, making a group. 
    * 
    * @param activity 
    *   Current View (or Activity) to which those RadioButtons 
    *   belong. 
    * @param radiosIDs 
    *   One RadioButton or more. 
    */ 
    public GRadioGroup(View activity, int... radiosIDs) { 
     super(); 

     for (int radioButtonID : radiosIDs) { 
      RadioButton rb = (RadioButton)activity.findViewById(radioButtonID); 
      if (rb != null) { 
       this.radios.add(rb); 
       rb.setOnClickListener(onClick); 
      } 
     } 
    } 

    /** 
    * This occurs everytime when one of RadioButtons is clicked, 
    * and deselects all others in the group. 
    */ 

    public void addRadioButtonsToGroup(RadioButton rb){ 
     radios.add(rb); 
     rb.setOnClickListener(onClick); 
    } 

    OnClickListener onClick = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      // let's deselect all radios in group 
      for (RadioButton rb : radios) { 

       ViewParent p = rb.getParent(); 
       if (p.getClass().equals(RadioGroup.class)) { 
        // if RadioButton belongs to RadioGroup, 
        // then deselect all radios in it 
        RadioGroup rg = (RadioGroup) p; 
        rg.clearCheck(); 
       } else { 
        // if RadioButton DOES NOT belong to RadioGroup, 
        // just deselect it 
        rb.setChecked(false); 
       } 
      } 

      // now let's select currently clicked RadioButton 
      if (!v.getClass().equals(RadioButton.class)) { 
       RadioButton rb = (RadioButton) v; 
       rb.setChecked(true); 
      } 

     } 
    }; 

    /** 
    * 
    ** Returns the Id of the radio button that is checked or -1 if none are checked 
    * 
    * @return 
    */ 
    public int getCheckedRadioButtonId() { 
     int checkedId = -1; 
     // Loop each radio button 
     for (RadioButton rb : radios) { 
      if (rb.isChecked()) 
       return rb.getId(); 
     } 
     return checkedId; 
    } 

    public void setCheckedRadioButton(int pos) { 
     // let's deselect all radios in group 
     for (RadioButton rb : radios) { 

      ViewParent p = rb.getParent(); 
      if (p.getClass().equals(RadioGroup.class)) { 
       // if RadioButton belongs to RadioGroup, 
       // then deselect all radios in it 
       RadioGroup rg = (RadioGroup) p; 
       rg.clearCheck(); 
      } else { 
       // if RadioButton DOES NOT belong to RadioGroup, 
       // just deselect it 
       rb.setChecked(false); 
      } 
     } 
     radios.get(pos).setChecked(true); 
    } 


    public void setCheckedRadioButtonDefault() { 
     radios.get(0).setChecked(true); 
    } 

    @Override 
    public void finalize() { 
     radios.clear(); 
    } 

} 

我需要添加單選按鈕所以程式設計讓我radiobutton.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layoutDirection="rtl" 
    android:paddingStart="@dimen/padding_right_rests_columns" 
    android:paddingEnd="@dimen/padding_left_rest_column" 
    android:textAlignment="center" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:layout_margin="5dp"> 

</RadioButton> 

以及添加(這是一個片段):

GRadioGroup gr = new GRadioGroup(); 
    RadioButton radioButton = (RadioButton) getActivity().getLayoutInflater().inflate(R.layout.radiobutton, null);//initialize and set content 
radioButton.setText("HEY") 
// And all the settings you want like position ... 
//finally 
gr.addRadioButtonsToGroup(radioButton); 

缺點?如果活動/片段/任何內容被破壞,您必須使用捆綁或意向處理選定的radiogroup。

希望能幫到你!