【问题标题】:How can I have a Radio Group divided in 2 columns and 5 lines,containing 10 Radio Buttons?如何将一个单选组分为 2 列和 5 行,包含 10 个单选按钮?
【发布时间】:2015-07-12 03:10:15
【问题描述】:

当我将单选组设置为水平时,它只有 2 个单选按钮的空间,而当我将它设置为垂直时,我有任意数量的空间,但每行只有一个。我想要的是每行 2 个单选按钮,其中 5 行,都在同一个单选组内,我该怎么做?

这就是我想要的样子:

【问题讨论】:

  • 您希望使用哪种编程语言实现这一目标?
  • @rajat C# xamarin android

标签: c# android xamarin radio-button radio-group


【解决方案1】:

这个解决方案有点奇怪,但它会起作用。

您将使用的视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"
        android:weightSum="2">

        <RadioGroup
            android:id="@+id/Radio1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 1" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 2" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 3" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 4" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 5" />
        </RadioGroup>

        <RadioGroup
            android:id="@+id/Radio2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 6" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 7" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 8" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 9" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RADIO 10" />
        </RadioGroup>
    </LinearLayout>
    <Button
        android:id="@+id/MyButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CHECK"
        />
</LinearLayout>

还有这样的活动:

public class MainActivity : Activity
{
    private RadioGroup _rg1;
    private RadioGroup _rg2;
    private Button _btn;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        _rg1 = FindViewById<RadioGroup>(Resource.Id.Radio1);
        _rg2 = FindViewById<RadioGroup>(Resource.Id.Radio2);
        _rg1.ClearCheck();
        _rg2.ClearCheck();
        _rg1.CheckedChange += OnRg1Change;
        _rg2.CheckedChange += OnRg2Change;

        _btn = FindViewById<Button>(Resource.Id.MyButton);
        _btn.Click += (sender, args) =>
        {
            var check1 = _rg1.CheckedRadioButtonId;
            var check2 = _rg2.CheckedRadioButtonId;
            var realCheck = check1 == -1
                ? check2
                : check1;
            Toast.MakeText(this, realCheck.ToString(), ToastLength.Long).Show();
        };
    }

    private void OnRg2Change(object sender, RadioGroup.CheckedChangeEventArgs e)
    {
        if (e.CheckedId == -1) return;

        _rg1.CheckedChange -= OnRg1Change;
        _rg1.ClearCheck();
        _rg1.CheckedChange += OnRg1Change;
    }

    private void OnRg1Change(object sender, RadioGroup.CheckedChangeEventArgs e)
    {
        if (e.CheckedId == -1) return;

        _rg2.CheckedChange -= OnRg2Change;
        _rg2.ClearCheck();
        _rg2.CheckedChange += OnRg2Change;
    }
}

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2015-01-11
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多