0
こんにちは私は2つのradiogroupを使用してこれを行うことを達成したので、radiogroup 2x2を作成したいと思います。しかし、問題は今、私はただ一つではなく二つのラジオボタンを選択することができます。私はこれをどうやってやろうとしますか?A radiogroup 2x2を作成するにはどうすればよいですか?
私は正確に開発するためにコットリンを使用します。
ありがとうございました!
こんにちは私は2つのradiogroupを使用してこれを行うことを達成したので、radiogroup 2x2を作成したいと思います。しかし、問題は今、私はただ一つではなく二つのラジオボタンを選択することができます。私はこれをどうやってやろうとしますか?A radiogroup 2x2を作成するにはどうすればよいですか?
私は正確に開発するためにコットリンを使用します。
ありがとうございました!
ここでは非常によく似た質問がありますgridlayout 3x3 あなたは思っていないことができないので、上の質問の回答を1つaceptする必要があります。私が何をした:
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();
}
}
私はprogramticallyので、私のradiobutton.xmlを作るラジオボタンを追加する必要があります。
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);
短所を:
<?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>
そして(これがフラグメントである)追加するための
?アクティビティ/フラグメント/何が破壊された場合は、バンドルまたはインテントで選択したradiogroupを処理する必要があります。お手伝いします!
このようなことはありません。 RadioGroupはLinearLayoutを継承しているため、** ** ** **または** **のいずれかになります。 –
2x2ラジオボタンを作成したい!私はそれが明らかに存在すると思います! –
** 2つのRadioButtonのグリッドを自由に設定できます。しかし、あなたはRadioGroupによって与えられた "相互排他性"を持っていません。 –