【问题标题】:Show many radio buttons显示许多单选按钮
【发布时间】:2015-04-10 06:18:06
【问题描述】:

我在一个单选组下有 7 个单选按钮。我使用orientation = "horizontal" 水平渲染它们。但一次只能看到 4 个,其余的则不可见。

有没有什么办法可以在第二行显示其余的按钮,同时将所有这些“单选按钮”保留在一个“单选按钮”中?

如果你需要我的代码 -

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

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

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1.5 BHK" />

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

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
</RadioGroup>

【问题讨论】:

  • 为什么要将它们全部保存在一个 RadioGroup 中(在布局文件中)?您可以轻松地在源中实现一个无线电组,即保留该组的所有radiobutton的列表,并设置侦听器,该侦听器在选中一个按钮时禁用组中所有其他RadioButtons。
  • 嗯,这听起来像是一个解决方案,但不是一个干净的解决方案。我认为必须有一些解决方案来满足我的需求。
  • @Hasina 您已使用相同问题的链接将此问题标记为重复:)

标签: android android-radiobutton


【解决方案1】:

您不能将RadioButtons 放在屏幕上的不同位置,同时将它们保持在同一个RadioGroup 中。您也不能将它们放在不同的行中(就像您想要实现的那样)。是的,我也讨厌。

您可以先制作复选框,按照您想要的方式放置它们,然后在每个复选框上使用setOnCheckedChangedListener。因此,如果其中一个被选中,请在其他人上调用setChecked(false)

使用自定义可绘制对象,使它们看起来像单选按钮而不是复选框。

【讨论】:

  • 不想把它们放在不同的地方,它们不适合排成一排,所以想把它们放在第二排,更像是'文本重排'的东西。
  • 我明白这一点。你的案例是我上面所说的一个子案例。在同一个无线电组中有两个不同的方向与将它们放置在不同的位置相同。
  • 我也试试这个,但你可以在没有任何 RadioGroup 的情况下轻松显示在屏幕上,只需拖动单选按钮
【解决方案2】:

您可以在这样的单选组中使用滚动视图

                 <RadioGroup
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal">
                <HorizontalScrollView
                   android:id="@+id/ScrlView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" >

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

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1.5 BHK" />

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

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                     </ScrollView>
</RadioGroup>

【讨论】:

  • 它将使其可滚动,但用户仍然看不到视图,因此不是直观的设计。
  • 我觉得你也可以用gridview代替scrollview
  • 好吧@Darpan 这也可能不是一个好方法,因为 android 设备的屏幕尺寸从 3.5" 到 45" 不等,所以我强烈反对水平使用单选按钮,你永远不知道 4 个选项总是适合在水平..您必须使用垂直单选组。强烈推荐
【解决方案3】:

我想,没有足够的地方容纳所有人。尝试将 android:layout_width="0dp"android:layout_weight="1" 设置为所有 RadioButtons。但它可能不是很漂亮。

【讨论】:

    【解决方案4】:

    尝试为所有单选按钮设置宽度

     android:layout_width="0dp"
    

    还为每个单选按钮添加android:layout_weight="1"

     <RadioButton
                     android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="1 BHK" 
    android:layout_weight="1"/>
    

    【讨论】:

      【解决方案5】:

      用你的 xml 代码替换下面的代码

      <HorizontalScrollView
          android:layout_width="match_parent"
          android:layout_height="wrap_content" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal" >
      
              <RadioGroup
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal" >
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="1 BHK" />
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="1.5 BHK" />
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="2 BHK" />
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="2.5 BHK" />
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="2 BHK" />
      
                  <RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="2.5 BHK" />
              </RadioGroup>
          </LinearLayout>
      </HorizontalScrollView>
      

      【讨论】:

        【解决方案6】:

        我也在RadioGroup 中尝试过这个,你一次只能选择一个,效果很好:

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

        【讨论】:

        • 这样你可以一次选择所有的单选按钮!这不是单选按钮的用途。 Google RadioGroup 及其作用。
        • 我也尝试过 RadioGroup,你一次只能选择一个
        【解决方案7】:

        我的解决方案是在 RadioGroup 布局中添加一个 LinearLayout。 我将 RadioGroup 的方向更改为垂直并添加 两个水平方向的 LinearLayout 将作为行提供服务。

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"        
            >
        
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1 BHK" />
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1.5 BHK" />
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />
        
            </LinearLayout>
        
        
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
        
                 <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3 BHK" />
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3.5 BHK" />
        
            </LinearLayout>
        </RadioGroup>
        

        【讨论】:

        • 这样您可以一次选择所有单选按钮!这不是单选按钮的用途。
        猜你喜欢
        • 2018-01-23
        • 2014-01-25
        • 2020-06-29
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多