我如何创建一个2 x 2的radiogroup?

你好,我想创建一个radiogroup 2×2,所以我实现了这个使用两个radiogroup。 但现在的问题是我可以选择两个单选按钮,而不是只有一个。我该怎么做到这一点?

我精确地使用kotlin来开发。

谢谢 !

在这里,你有一个非常类似的问题gridlayout 3×3因为你不能想要你想要trivialy,你应该接受上面的问题的答案1。 我做了什么:

 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。

希望能帮到你!