安卓中的多行单选按钮?

19

我在尝试将单选按钮放置在多行中遇到了问题。

以下是我的XML代码:

           <RadioGroup android:layout_width="fill_parent"
               android:layout_height="wrap_content"
                android:orientation="vertical"
                 >
  <RadioGroup android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         >
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/radio_one0Id"
          android:textSize="13sp"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
       />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
            android:textSize="13sp"
          android:text="5%" 
          android:id="@+id/radio_one5Id"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
         />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="10%" 
          android:textSize="13sp"
          android:layout_weight="1"
          android:id="@+id/radio_one10Id"
          android:onClick="oneRadioButtonClicked"
       />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="20%" 
          android:layout_weight="1"
          android:textSize="13sp"
          android:onClick="oneRadioButtonClicked"
          android:id="@+id/radio_one20Id"
         />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="35%" 
          android:id="@+id/radio_one35Id"
          android:textSize="13sp"
          android:onClick="oneRadioButtonClicked"
          android:layout_weight="1"
       />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="50%" 
          android:textSize="13sp"
          android:id="@+id/radio_one50Id"
          android:onClick="oneRadioButtonClicked"
          android:layout_weight="1"
         />

      </RadioGroup>

        <RadioGroup android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
         <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="65%" 
          android:textSize="13sp"
          android:id="@+id/radio_one65Id"
          android:onClick="oneRadioButtonClicked"
          android:layout_weight="1"
       />
      <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="75%" 
          android:textSize="13sp"
          android:layout_weight="1"
          android:id="@+id/radio_one75Id"
          android:onClick="oneRadioButtonClicked"
         />
       <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="85%" 
          android:textSize="13sp"
          android:id="@+id/radio_one85Id"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
         />
        <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
         android:textSize="13sp"
          android:text="95%" 
          android:id="@+id/radio_one95Id"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
         />
         <RadioButton
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="100%" 
          android:id="@+id/radio_one100Id"
          android:textSize="13sp"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
         />
                       </RadioGroup>
      </RadioGroup>

这是代码

public void oneRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radio_one0Id:
            if (checked)
                one = "0";
            break;
        case R.id.radio_one5Id:
            if (checked)
                one = "5";
            break;
        case R.id.radio_one10Id:
            if (checked)
                one = "10";
            break;
        case R.id.radio_one20Id:
            if (checked)
                one = "20";
            break;
        case R.id.radio_one35Id:
            if (checked)
                one = "35";
            break;
        case R.id.radio_one50Id:
            if (checked)
                one = "50";
            break;
        case R.id.radio_one65Id:
            if (checked)
                one = "65";
            break;
        case R.id.radio_one75Id:
            if (checked)
                one = "75";
            break;
        case R.id.radio_one85Id:
            if (checked)
                one = "85";
            break;
        case R.id.radio_one95Id:
            if (checked)
                one = "95";
            break;
        case R.id.radio_one100Id:
            if (checked)
                one = "100";
            break;
         default:
             System.out.println("default");
    }
}

这将会显示为enter image description here

它将会选中两行中的两个按钮,我希望只能选中其中一行的一个按钮,在此感谢任何帮助。


1
https://dev59.com/jmkv5IYBdhLWcg3wsCtO#22465700 - Kristy Welsh
6个回答

15

添加一个具有垂直方向的单选按钮组,并添加两个LinearLayout:

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

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" >

      <RadioButton
          android:id="@+id/radio_one0Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:textSize="13sp" />

      <RadioButton
          android:id="@+id/radio_one5Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:text="5%"
          android:textSize="13sp" />

      <RadioButton
          android:id="@+id/radio_one10Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:text="10%"
          android:textSize="13sp" />

      <RadioButton
          android:id="@+id/radio_one20Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:text="20%"
          android:textSize="13sp" />

      <RadioButton
          android:id="@+id/radio_one35Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:text="35%"
          android:textSize="13sp" />

      <RadioButton
          android:id="@+id/radio_one50Id"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="oneRadioButtonClicked"
          android:text="50%"
          android:textSize="13sp" />

  </LinearLayout>

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_weight="1"
         android:orientation="horizontal" >

         <RadioButton
             android:id="@+id/radio_one65Id"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="oneRadioButtonClicked"
             android:text="65%"
             android:textSize="13sp" />

         <RadioButton
             android:id="@+id/radio_one75Id"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="oneRadioButtonClicked"
             android:text="75%"
             android:textSize="13sp" />

         <RadioButton
             android:id="@+id/radio_one85Id"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="oneRadioButtonClicked"
             android:text="85%"
             android:textSize="13sp" />

         <RadioButton
             android:id="@+id/radio_one95Id"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="oneRadioButtonClicked"
             android:text="95%"
             android:textSize="13sp" />

         <RadioButton
             android:id="@+id/radio_one100Id"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="oneRadioButtonClicked"
             android:text="100%"
             android:textSize="13sp" />

     </LinearLayout>

  </RadioGroup>

完美而简单的解决方案 - Amir Ismail
52
这个解决方案没有满足原始帖子中的一个要求,即它应该每次只能选择一行中的一个按钮:“它将选择2行中的两个按钮,我想它只选其中一行中的一个按钮”。RadioButton必须是RadioGroup的直接子项才能被视为该组的一部分。 - BasicPleasureModel
它不起作用。请检查下面的答案:https://dev59.com/TmQm5IYBdhLWcg3w4CTA#54252118 那个应该被接受。 - Pratik Butani
我正在使用RecyclerView,其中的项是RadioButton。当选择一个项目时,我会取消选择先前的RadioButton。 - kirkadev

8
从搜索结果来看,似乎没有一种方法可以做到这一点,这意味着您将不得不手动实现此布局行为。两个可能的选项是:
  1. 创建 RadioGroup 的副本以扩展不同的布局, 或至少允许您在动态控制它。
  2. 实现自己的定制布局来替换 RadioGroup,该布局扩展了您选择的布局,并实现 OnClickListener。这里有一个很好的例子 如何将 3x3 格网的单选按钮分组?

8
我进行了大量的研究,终于找到了一个解决方案。如果你想要像这样的东西:

enter image description here

首先,您需要下载/创建一个新的类,如link所示,因为RadioGroup默认使用LinearLayout。现在,您有了使用RelativeLayout的RadioGroup。唯一剩下的事情就是通过使用一个巧妙的小技巧按百分比分离单选按钮(就像使用weightSum一样,只是在RelativeLayout中没有weightSum,只有LinearLayout):

<rs.cdl.attendance.UI.RelativeRadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <View
        android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <RadioButton
        android:id="@+id/start_radio_button"
        android:layout_alignRight="@id/strut"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

    <RadioButton
        android:id="@+id/finish_radio_button"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Finish" />

    <RadioButton
        android:id="@+id/pause_radio_button"
        android:layout_alignRight="@id/strut"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/start_radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />

    <RadioButton
        android:id="@+id/continue_radio_button"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:layout_below="@id/finish_radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Continue" />

</rs.cdl.attendance.UI.RelativeRadioGroup>

这是本页面中最好的答案之一。链接加一。 - Tejas Pandya

5

这对我有用。 第一行(NameRadioGroupe2.clearCheck();)清除了其他Radiogroup,第二行在选中的按钮中添加了一个勾号

public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

switch(view.getId()) {
    case R.id.radio_one0Id: {  
        one = "0";      
        NameRadioGroupe2.clearCheck();
        NameRadioGroupe1.check(view.getId());
        break;
    }
            break;
    case R.id.radio_one5Id: {
        NameRadioGroupe2.clearCheck();
        NameRadioGroupe1.check(view.getId());          
        one = "5";
        break;
        }
        .
        .
        .
        .
        .
               case R.id.radio_one65Id: {
        NameRadioGroupe1.clearCheck();
        NameRadioGroupe2.check(view.getId());          
        one = "65";
        break;
        }
    case R.id.radio_one75Id: {
        NameRadioGroupe1.clearCheck();
        NameRadioGroupe2.check(view.getId());          
        one = "75";
        break;
        }
        .
        .
        .
        .
        .

1
我试图解决同样的问题。
我的解决方法是在各自的LinearLayout中添加多个RadioGroups。当从另一个RadioGroup选择一个单选按钮时,为了确保第一个RadioGroup按钮不再被选中,我在单选按钮的.Click函数中添加了.Checked = false。然后,因为一开始我遇到了错误,有时新点击的单选按钮不会被选中,所以我在实际的单选按钮上添加了.Checked = true。
我的XML
<LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:padding="10dp">
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="3"
                style="@style/radios"
                android:orientation="horizontal">
                <RadioButton
                    android:id="@+id/rad1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    style="@style/radios"
                    android:text="1"
                    android:checked="true" />
                <RadioButton
                    android:id="@+id/rad2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    style="@style/radios"
                    android:text="2" />
                <RadioButton
                    android:id="@+id/rad3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    style="@style/radios"
                    android:text="3" />
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:padding="10dp">
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="3"
                style="@style/radios"
                android:orientation="horizontal">
                <RadioButton
                    android:id="@+id/rad4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    style="@style/radios"
                    android:text="4" />
                <RadioButton
                    android:id="@+id/rad5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    style="@style/radios"
                    android:text="5" />
            </RadioGroup>
        </LinearLayout>

然后是我的C#

            var radio1 = FindViewById<RadioButton>(Resource.Id.rad1);
            var radio2 = FindViewById<RadioButton>(Resource.Id.rad2);
            var radio3 = FindViewById<RadioButton>(Resource.Id.rad3);
            var radio4 = FindViewById<RadioButton>(Resource.Id.rad4);
            var radio5 = FindViewById<RadioButton>(Resource.Id.rad5);

            radio1.Click += delegate
            {
                radio2.Checked = false;
                radio3.Checked = false;
                radio4.Checked = false;
                radio5.Checked = false;
                radio1.Checked = true;
            };

            radio2.Click += delegate
            {
                radio1.Checked = false;
                radio3.Checked = false;
                radio4.Checked = false;
                radio5.Checked = false;
                radio2.Checked = true;
            };

            radio3.Click += delegate
            {
                radio1.Checked = false;
                radio2.Checked = false;
                radio4.Checked = false;
                radio5.Checked = false;
                radio3.Checked = true;
            };

            radio4.Click += delegate
            {
                radio1.Checked = false;
                radio2.Checked = false;
                radio3.Checked = false;
                radio5.Checked = false;
                radio4.Checked = true;
            };

            radio5.Click += delegate
            {
                radio1.Checked = false;
                radio2.Checked = false;
                radio3.Checked = false;
                radio4.Checked = false;
                radio5.Checked = true;
            };

原始的,但对我有效。

0

制作多行单选按钮的一种简单方法是使用MultiLineRadioGroup库。它支持任意行和列。

使用方法很简单: 在您项目的build.gradle文件中添加:

allprojects {
    repositories {
        ...
        maven { 
            url "https://jitpack.io"
        }
        ...
    }
}

在您的应用程序或模块的 build.gradle 文件中添加以下内容:
dependencies {
    ...
    compile 'com.github.Gavras:MultiLineRadioGroup:v1.0.0.6'
    ...
}

您可以在XML中使用字符串数组资源来创建视图,如下所示:

在布局的XML中添加:

<com.whygraphics.multilineradiogroup.MultiLineRadioGroup xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_activity_multi_line_radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        multi_line_radio_group:default_button="button_2"
        multi_line_radio_group:max_in_row="3"
        multi_line_radio_group:radio_buttons="@array/radio_buttons" />

在arrays.xml中添加:

<string-array name="radio_buttons">
        <item>button_1</item>
        <item>button_2</item>
        <item>button_3</item>
        <item>button_4</item>
        <item>button_5</item>
</string-array>

或者通过编程方式添加它们:

mMultiLineRadioGroup.addButtons("button to add 1", "button to add 2", "button to add 3");

这个和我试过的其他东西一样,也不会自动换行。你可以通过 max_in_row 固定按钮数量,但这只适用于手动决定行数的情况。它不像 Flexbox 那样可以自动换行。 - Damn Vegetables

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接