【问题标题】:How to set only one RadioButton Can be selected at the time in RadioGroup如何只设置一个 RadioButton 可以在 RadioGroup 中当时选择
【发布时间】:2016-09-15 18:19:19
【问题描述】:

我在radiogroup中创建了一个单选按钮,但是当我尝试运行应用程序时,所有单选按钮都可以一直选择,如何设置一次只能选择一个单选按钮?

我正在使用片段

RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // find which radio button is selected
                if(checkedId == R.id.Abdominal) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: A",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Arm) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: B",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Back){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: C",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Chest){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: D",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Leg){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: E",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Shoulder){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: F",
                            Toast.LENGTH_SHORT).show();
                }
            }

        });

这里是 RG 和 RB 的 xml 代码

<RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/RGroup">

                    <TableRow android:weightSum="1">
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Abdominal"
                        android:id="@+id/Abdominal"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Arm"
                        android:id="@+id/Arm"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Back"
                        android:id="@+id/Back" />
                        </TableRow>
                    <TableRow>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Chest"
                            android:id="@+id/Chest"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Leg"
                            android:id="@+id/Leg"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Shoulder"
                            android:id="@+id/Shoulder"/>
                    </TableRow>
                </RadioGroup>

已编辑 1:答案: 如果你不想单选按钮可以一次性选中,所以不要使用Tablerow

【问题讨论】:

    标签: android android-radiogroup android-radiobutton


    【解决方案1】:

    由于 RadioGroup 中的 TableRow,它无法正常工作。由于它们之间的 TableRow,所有 RadioButton 都没有组合在一起。

    RadioButton 应该是 RadioGroup 的直接子级,否则分组不起作用。

    只需像这样更改您的代码即可:

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/RGroup">
    
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Abdominal"
                    android:id="@+id/Abdominal"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Arm"
                    android:id="@+id/Arm"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Back"
                    android:id="@+id/Back" />                                        
    
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Chest"
                    android:id="@+id/Chest"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Leg"
                    android:id="@+id/Leg"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Shoulder"
                    android:id="@+id/Shoulder"/>
    
            </RadioGroup>
    

    希望这会有所帮助。 :)

    【讨论】:

    • 哇,谢谢!这就是答案,所有按钮都可以在 tablerow 中选择一次 :) 感谢您的帮助
    • 顺便说一句,我使用 TableRow 在 2 行中制作单选按钮,那么如何在没有 tablerow 的情况下使其成为 2rwos?
    【解决方案2】:

    我注意到如果不将 id 设置为单选按钮,单选将不起作用。

                 <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <RadioButton
                        android:id="@+id/expenseRadio"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="@string/expense" />
    
                    <RadioButton
                        android:id="@+id/incomeRadio"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/income" />
                </RadioGroup>
    

    【讨论】:

      【解决方案3】:
        <RadioGroup
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:id="@+id/radioGroup">
              <RadioButton
                  android:layout_width="0dp"
                  android:layout_weight="50"
                  android:layout_height="wrap_content"
                  android:text="Debit"
                  android:id="@+id/rDebit"
                  android:checked="false"
                   />
      
              <RadioButton
                  android:layout_width="0dp"
                  android:layout_weight="50"
                  android:layout_height="wrap_content"
                  android:text="Credit"
                  android:id="@+id/rCredit"
                  android:checked="false" />
      
          </RadioGroup>
      

      在java文件中

       RadioGroup radioGroup;
      
      
      
      radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
      

      什么时候做某事

       if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
      {
      // do something
      }
      

      【讨论】:

      • 按照您的代码,但仍然可以一次性选择所有单选按钮,我使用的是片段 btw,
      【解决方案4】:

      我遇到了同样的问题,发现我在哪里犯了错误,我在下面描述它:

      1. 如果您在 RadioGroup 中使用 RadioButtons,那么 RadioButtons should be direct child of RadioGroup

      2. 检查您的第一个条件,如果正确,然后give id to RatioButton 并运行项目,它将自动解决您的问题。

      【讨论】:

        【解决方案5】:

        简单的方法。单击单选按钮。按照下面的代码做。

        public void clearRadioChecked() {
            rdopa.setChecked(false);
            rdopb.setChecked(false);
            rdopc.setChecked(false);
            rdopd.setChecked(false);
        }
        

        如果您想选择 rdopa,然后单击 rdopa,请执行以下操作。

        clearRadioChecked()
        rdopa.setChecked(true);
        

        【讨论】:

        • 我们最好使用 CheckBoxes 然后.. 您没有使用 RadioButton 手动检查和取消选中它。默认行为是一个可以立即选中,另一个将自动取消选中
        • @janki gadhiya,如果你有一组单选按钮并且你想重复使用很多次,那么你需要实现上述方式。否则只有一组单选按钮,然后根据你的概念它可以正常工作自动检查/取消选中。
        【解决方案6】:

        您可以在 RadioGroup 上使用 android:checkedButton 属性,提供您最初要检查的 RadioButtonid 并选择另一个 RadioButton 将清除之前的选择。

        <RadioGroup
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:checkedButton="@+id/rbNo"
             android:orientation="horizontal">
        
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="50dp"
                    android:text="Yes" />
        
                <RadioButton
                    android:id="@+id/rbNo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No" />
        </RadioGroup>
        

        【讨论】:

          【解决方案7】:

          您可以创建自己的RadioGroup,它能够找到嵌套在您的组中的所有RadioButtons,无论是否直接子。在我的 NestedRadioGroup 的代码下方。在 xml 文件中使用它而不是 RadioGroup 应该可以解决问题。

          public class NestedRadioGroup extends LinearLayout {
              private SparseArray<RadioButton> radioButtons;
              private int checkedId;
          
              public NestedRadioGroup(Context context) {
                  this(context, null);
              }
          
              public NestedRadioGroup(Context context, AttributeSet attrs) {
                  super(context, attrs);
                  radioButtons = new SparseArray<>();
                  checkedId = -1;
              }
          
              @Override
              public void addView(View child, int index, ViewGroup.LayoutParams params) {
                  super.addView(child, index, params);
                  findRadioButtons(child);
              }
          
              private void findRadioButtons(View child) {
                  if (child instanceof RadioButton) {
                      RadioButton newButton = (RadioButton) child;
                      newButton.setId(radioButtons.size());
                      newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
                          if (isChecked) {
                              if (checkedId != -1) {
                                  radioButtons.get(checkedId).setChecked(false);
                              }
                              radioButtons.get(buttonView.getId()).setChecked(true);
                              checkedId = buttonView.getId();
                          }
                      });
                      radioButtons.put(newButton.getId(), newButton);
                  } else if (child instanceof ViewGroup) {
                      ViewGroup group = (ViewGroup) child;
          
                      for (int i = 0; i < group.getChildCount(); i++) {
                          this.findRadioButtons(group.getChildAt(i));
                      }
                  }
              }
          }
          

          有什么让代码更简洁的想法吗?

          希望对你有用:)

          【讨论】:

          • 正是我想要的,非常感谢你的儿子 :)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-25
          • 1970-01-01
          • 1970-01-01
          • 2014-03-18
          相关资源
          最近更新 更多