你好我想创建一个2x2的无线电组,所以我用两个radiogroup来做到这一点。但问题是现在我可以选择两个单选按钮而不是一个..我怎么能尝试这样做?
我确切地说我使用kotlin来开发。
谢谢!
答案 0 :(得分:0)
这里有一个非常相似的问题gridlayout 3x3 因为你不能想要琐事,你应该接受上述问题的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,以便制作我的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);
缺点?如果活动/片段/任何被破坏的话,你必须用束或意图处理选定的放射性组。
希望能帮到你!