【问题标题】:Cannot uncheck radiobutton when used inside two linear layout在两个线性布局中使用时无法取消选中单选按钮
【发布时间】:2018-09-18 12:14:18
【问题描述】:

我使用两个线性布局来显示两行单选按钮 3 在一行中,3 在另一行中。我搜索发现应该使用radio group。但是在使用单选组后,当我选择单选按钮时,我也无法取消选中单选按钮

<RadioGroup
    android:id="@+id/payment_mode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp"
    android:orientation="vertical">

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

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/cash"
            android:text="CASH" />

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/card"
            android:text="CARD" />

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/pay_tm"
            android:text="PAYTM" />

    </LinearLayout>

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

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/bank"
            android:text="BANK" />

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/cc_av"
            android:text="CC-AV" />

        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/credit"
            android:text="CREDIT" />

    </LinearLayout>

</RadioGroup>

【问题讨论】:

  • 请添加您的代码

标签: android xml radio-button radio-group


【解决方案1】:

来自开发者网站

要创建每个单选按钮选项,请在您的 布局。但是,由于单选按钮是互斥的,您 必须在 RadioGroup 中将它们组合在一起。通过将它们分组 一起,系统确保只有一个单选按钮可以 一次选择。

所以单选按钮的直接父级必须是 RadioGroup ,在您的布局文件中,直接父级是 LinearLayout。移除 LinearLayout 并将单选按钮直接放在 RadioGroup 下。

【讨论】:

    【解决方案2】:

    您不能在RadioGroup 中使用任何布局。你只能使用 RadioButton,否则RadioButton 将不起作用。 RadioButton 应该是 RadioGroup 的直接子级。

    您的问题将通过将 radioGroup 的方向更改为水平来解决。 更多信息here

    【讨论】:

      【解决方案3】:

      对于我的回答,您无需使用广播组。您只需在每个单选按钮上设置点击侦听器。例如。

      RadioButton tempRadioButton;   //global variable.
      radioButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          if (tempRadioButton != null){
                              tempRadioButton.setChecked(false);
                          }
                          tempRadioButton = (RadioButton) view;
                      }
                  });
      

      您也可以使用此方法在列表视图或回收器视图的每个项目上设置单选按钮。

      【讨论】:

        【解决方案4】:

        尝试像这样更改您的布局并在点击操作时参考此link

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
        
                <RadioGroup
                    android:id="@+id/payment_mode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="18dp"
                    android:orientation="vertical">
        
                    <RadioButton
                        android:id="@+id/cash"
                        style="@style/LinkButton"
        
                        android:text="CASH" />
        
                    <RadioButton
                        android:id="@+id/card"
                        style="@style/LinkButton"
                        android:text="CARD" />
        
                    <RadioButton
                        android:id="@+id/pay_tm"
                        style="@style/LinkButton"
                        android:text="PAYTM" />
        
                    <RadioButton
                        android:id="@+id/bank"
                        style="@style/LinkButton"
                        android:text="BANK" />
        
        
                    <RadioButton
                        android:id="@+id/cc_av"
                        style="@style/LinkButton"
                        android:text="CC-AV" />
        
        
                    <RadioButton
                        android:id="@+id/credit"
                        style="@style/LinkButton"
                        android:text="CREDIT" />
        
                </RadioGroup>
            </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 2016-08-28
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 2016-05-28
          • 2013-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多