我怎样才能将一个单选组分成 2 列和 5 行,包含 10 个单选按钮?
How can I have a Radio Group divided in 2 columns and 5 lines,containing 10 Radio Buttons?
当我将单选组设置为水平时,它只有 space 用于 2 个单选按钮,而当我将其设置为垂直时,我有任意数量的 space 但只有每行一个。我想要的是每行 2 个单选按钮和 5 行,都在同一个单选组中,我该怎么做?
这就是我想要的样子:
这个解决方案有点奇怪,但它会起作用。
对于视图,您将使用:
<?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>
和 activity 这样:
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;
}
}
当我将单选组设置为水平时,它只有 space 用于 2 个单选按钮,而当我将其设置为垂直时,我有任意数量的 space 但只有每行一个。我想要的是每行 2 个单选按钮和 5 行,都在同一个单选组中,我该怎么做?
这就是我想要的样子:
这个解决方案有点奇怪,但它会起作用。
对于视图,您将使用:
<?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>
和 activity 这样:
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;
}
}