【问题标题】:How to force RadioGroup to select only one RadioButton at a time programatically?如何强制 RadioGroup 以编程方式一次只选择一个单选按钮?
【发布时间】:2014-03-18 03:03:51
【问题描述】:

我正在以编程方式设计自定义表单,用户可以在其中提出问题并可以使用单选按钮添加多个选项。我已经使用了 RadioGroup,并在其中添加了单选按钮。但是在选择时,我希望一次只选择一个单选按钮。如何以编程方式实现它。请帮帮我..

这是我的代码

final RadioGroup radioGroup = new RadioGroup(getApplicationContext());
radioGroup.setId(1);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT);

RadioButton radioButtonView = new  RadioButton(getApplicationContext());
radioButtonView.setId(i++);
radioButtonView.setText(addnew);

radioGroup.addView(radioButtonView, p);
loption.addView(radioGroup, p);

提前致谢,

【问题讨论】:

  • 请提供一些您尝试过的代码以及不适合您的代码
  • 您现在的实施有什么问题?如果您以编程方式尝试过任何事情
  • 那么,您是否为每个RadioButton 创建一个新的RadioGroup?如果所有RadioButtons 进入同一个RadioGroup,你应该得到你正在寻找的行为吗?还是代码中多个位置的代码 sn-p?
  • radioButtonView.setId(i++); i 到底在哪里?你在 for 循环中使用整个代码吗?
  • 没问题 :) 如果我的答案确实有效,如果你能将我的答案标记为解决方案,那就太好了。您之前提出的所有问题也是如此。

标签: android radio-button android-radiogroup


【解决方案1】:
 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.d(TAG, "onCheckedChanged  " + buttonView.getId());
        mRadioGroup.clearCheck();
        if (isChecked) {
            mRadioGroup.check(buttonView.getId());
        }
    }

【讨论】:

    【解决方案2】:

    您似乎正在为每个RadioButton 创建一个新的RadioGroup

    您应该将每个新的RadioButton 添加到同一个RadioGroup。然后RadioGroup 将确保当时只能选择一个RadioButton

    【讨论】:

      【解决方案3】:

      我在插入 LinearLayout 以将单选按钮放入网格时尝试过同样的问题。

      我是这样解决的。 考虑到我们有更多 RadioGroups,我们知道被按下的 RadioButton。出于这个原因,只需从您的所有 RadioGroups 中删除侦听器,然后取消选择所有 RadioButtons,将 onCheckedChangeListener 重置为您的 RadioGroup。

      这是我的代码:

      onCheckedChangeListener= new RadioGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                  clearAllRadioButton();
                  ((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
                  resetListenerRadioGroup();
              }
          };
      
          onCheckedChangeListener2 = new RadioGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                  clearAllRadioButton();
                  ((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
                  resetListenerRadioGroup();
              }
          };
      
      
          binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
          binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
      
      
      
      private void clearAllRadioButton() {
          binding.rg2.setOnCheckedChangeListener(null);
          binding.rg.setOnCheckedChangeListener(null);
          binding.failed.setChecked(false);
          binding.draft.setChecked(false);
          binding.sent.setChecked(false);
          binding.inbox.setChecked(false);
      }
      
      private void resetListenerRadioGroup(){
          binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
          binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
      }
      

      *不要使用radioButton的clearCheck(),因为它在重置监听器时反应不好。

      【讨论】:

        【解决方案4】:

        看看这个对你有用:

        首先获取 RadioButton:

        RadioButton radioBalls = (RadioButton) findViewById(R.id.radioGameInPlayBalls);
        RadioButton radioTime = (RadioButton) findViewById(R.id.radioGameInPlayTime);
        radioBalls.setOnCheckedChangeListener(this);
        radioTime.setOnCheckedChangeListener(this);
        

        现在在@overide method:

         @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean c) {
                switch (buttonView.getId()) {
                case R.id.radioGameInPlayBalls:
                    if (radioTime.isChecked() && c) {
                        radioBalls.setChecked(false);
                        return;
                    }
                    radioBalls.setEnabled(c);
                break;
            case R.id.radioGameInPlayTime:
                if (radioBalls.isChecked() && c) {
                    radioTime.setChecked(false);
                    return;
                }
                radioTime.setEnabled(c);
                break;
            default:
                break;
            }
        
        
        
        }
        

        【讨论】:

          【解决方案5】:

          您应该使用 RadioGroup 检查单选按钮,而不是直接检查单选按钮。下面,我从选项列表中填充单选组,并获取要检查的选项的单选按钮 ID,并在填充单选组后检查相应的按钮。如果您通过索引确定选中的选项,也是如此。

          int checkedId = -1; 
          
          for(JsonElement option : options){
          
              RadioButton radioButton = new RadioButton(getContext());
          
              radioGroup.addView(radioButton);
          
              if(checkedOptionId.equals(id)){
                  checkedId = radioButton.getId();
              }
          }
          
          radioGroup.check(checkedId);
          

          【讨论】:

            猜你喜欢
            • 2015-07-21
            • 2012-04-23
            • 1970-01-01
            • 2017-06-28
            • 1970-01-01
            • 2013-07-20
            • 1970-01-01
            • 2016-01-09
            • 1970-01-01
            相关资源
            最近更新 更多