【问题标题】:RadioButtons inside same RadioGroup but both buttons can be selected同一个 RadioGroup 内的 RadioButtons 但两个按钮都可以选择
【发布时间】:2016-04-07 03:14:11
【问题描述】:

我在同一个RadioGroup 中有两个RadioButtons。但是在每个RadioButton 旁边,我想要一个ImageButton(信息)。因此我的代码如下:

<RadioGroup>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
<RadioGroup/>

但现在我面临的问题相当简单,两个 RadioButtons 都可以选择。我希望一次只选择一个。

请忽略任何错别字。一定要通过手机发布。

【问题讨论】:

标签: android android-layout radio-button radio-group


【解决方案1】:

RadioGroups 是 LinearLayouts 本身,仅支持 RadioButtons 作为直接子代。你可以看到这种行为here

您尝试做的事情不可能像这样,因为您将RadioButton 包装在LinearLayout 中。

您有 2 种可能性:您可以复制代码并制作自己的 RadioGroup
或者您可以extend RadioButton 并通过在您的RadioButton 中应用您的自定义布局there 来更改其外观。

【讨论】:

    【解决方案2】:

    RadioGroup 不允许任何 ViewGroup 作为其子级。您必须使用 drawableRight 属性。

    【讨论】:

    • 我猜你没有得到这个问题。我会请你再读一遍。
    【解决方案3】:

    好吧,我确实找到了一个对我有用的解决方案,因为我的 RadioButtons 很少。

    我为每个 RadioButton 设置了一个 OnClickListener() 并设置其他未选中。

    final RadioButton a1 = (RadioButton) findViewById(R.id.a1);
    final RadioButton a2 = (RadioButton) findViewById(R.id.a2);
        a1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a2.setChecked(false);
            }
        });
        a2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a1.setChecked(false);
            }
        });
    

    【讨论】:

    • 我想我会使用同样的方法。但由于我有很多选择,我要做的是跟踪最后选择的单选按钮并在 OnClickListener() 中实现lastRadioButtonSelected.setChecked(false)。我想必须强力支持 Android SDK 才能按时完成工作!
    【解决方案4】:
    1. 您只能使用单选按钮组的子项
    2. 如果你不想要这个然后使用我的代码并删除单选组你可以使用自定义单选按钮

        final RadioButton a1 = (RadioButton) findViewById(R.id.a1);
       final RadioButton a2 = (RadioButton) findViewById(R.id.a2);
       a1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a2.setChecked(false);                    
            a1.setChecked(true);
      
        }
           });
         a2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          a1.setChecked(false);
          a2.setChecked(true);
      
      }
      });
      

    【讨论】:

      【解决方案5】:

      更新:我认为这会对您有所帮助。我有同样的问题。

      <LinearLayout> //Horizontal
          <LinearLayout>//Vertical
              <ImageButton/>
              <ImageButton/>
          </LinearLayout>
          <LinearLayout>//Vertical
              <RadioGroup> //Vertical
                  <RadioButton/>
                  <RadioButton/>
              </RadioGroup>
          </LinearLayout>
      </LinearLayout>
      

      1【最后会是这个样子】

      【讨论】:

      • 我不能 100% 确定这是否有帮助,因为您没有展示如何在此 RadioButtons 旁边放置 ImageButton
      • 你不需要 &lt;LinearLayout&gt;//Vertical 只需用 RadioGroup 替换它,因为 RadioGroup 已经是一个布局。但是,我想这种方法是一个 PITA 来排列单选按钮和图像
      • 我根据您的需要重新更新了答案。你能重新考虑投票吗?这样,当您单击单选按钮时,因为它们在单选组下,它将交换单击的变量。
      猜你喜欢
      • 2013-06-14
      • 2017-01-14
      • 2012-05-12
      • 2021-10-04
      • 2023-03-10
      • 2014-03-18
      • 2014-06-30
      • 2012-04-14
      相关资源
      最近更新 更多