3

画像のようにしようとして RadioGroupいます。一度に1つだけRadioButtonチェックする必要があります。

これは私がこれを達成するためにしたことです。

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio1" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio2" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio3" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio4" />
    </RadioGroup>
</RadioGroup>

ここに画像があります

私が欲しいもの

しかし、これを使用すると、各行から任意の 1 つを選択できます。つまり、2 つの項目を選択できます。このような:

ここに画像の説明を入力

私を助けてください...

4

1 に答える 1

10

ここに画像の説明を入力

更新しました

RadioButtonその他RadioButtonをクリックすると、動的に選択を解除できます。そうするために、私は に追加Viewして遊ぶViewGroup必要indexがありましたTableRow.

上の図に示すように、任意の時点で 1 つのラジオ ボタンのみを選択して、グリッドのようなレイアウトに配置する必要があります。以下のコードを参照してください。

.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.test.RadioButtonWithTableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TableRow>

        <RadioButton
            android:id="@+id/radio1"
            android:text="Radio 1" />

        <RadioButton
            android:id="@+id/radio3"
            android:text="Radio 3" />
    </TableRow>

    <TableRow>

        <RadioButton
            android:id="@+id/radio2"
            android:text="Radio 2" />

        <RadioButton
            android:id="@+id/radio4"
            android:text="Radio 4" >
        </RadioButton>
    </TableRow>

</com.example.test.RadioButtonWithTableLayout>

.java:

public class RadioButtonWithTableLayout extends TableLayout implements
        OnClickListener {

    private RadioButton mBtnCurrentRadio;

    public RadioButtonWithTableLayout(Context context) {
        super(context);
    }

    public RadioButtonWithTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onClick(View v) {
        final RadioButton mBtnRadio = (RadioButton) v;

        // select only one radio button at any given time
        if (mBtnCurrentRadio != null) {
            mBtnCurrentRadio.setChecked(false);
        }
        mBtnRadio.setChecked(true);
        mBtnCurrentRadio = mBtnRadio;
    }

    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        setChildrenOnClickListener((TableRow) child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        setChildrenOnClickListener((TableRow) child);
    }

    private void setChildrenOnClickListener(TableRow tr) {
        final int c = tr.getChildCount();
        for (int i = 0; i < c; i++) {
            final View v = tr.getChildAt(i);
            if (v instanceof RadioButton) {
                v.setOnClickListener(this);
            }
        }
    }
}

ソース: http://developer.android.com/reference/android/widget/RadioGroup.html http://developer.android.com/reference/android/widget/TableRow.html

于 2013-05-29T10:30:26.760 に答える