【问题标题】:RadioButton and Switch AndroidRadioButton 和 Switch Android
【发布时间】:2018-05-01 13:33:55
【问题描述】:

我有一个 Switch 和一个 RadioButton。我希望当我单击并选中 Switch 时,RadioButton 将被取消选中,反之亦然。我该怎么做?

我的代码是:

 Switch switchButton = new Switch(this);
 RadioButton radioButton = new RadioButton(this);

 switchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(switchButton.isChecked()){
                radioButton.setChecked(false);
            }
        }
    });


radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radioButton.isChecked()){
                    switchButton.setChecked(false);
                }
            }
        });

我的代码无法正常工作,请您帮帮我,谢谢!

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    您不必使用setOnClickListener(),但需要使用setnCheckedChangeListener()

    看下面的代码

    Switch switchButton = new Switch(this);
    RadioButton radioButton = new RadioButton(this);
    
    switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    radioButton.setChecked(false);
                }
            }
        });
    
    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    switchButton.setChecked(false);
                }
            }
        });
    

    【讨论】:

      【解决方案2】:

      您必须在switchButton 上使用OnCheckedChangeListener() 监听Switch中的状态变化

       switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if(isChecked){
                      switchButton.setChecked(false);
                  }
              }
          });
      

      在您的 RadioButton 上使用相同的侦听器

      【讨论】:

        【解决方案3】:

        您的视图中有按钮吗?如果这样做,您需要使用findViewById 函数绑定到该视图以更改按钮。如果您只是以编程方式创建一个按钮,它不会出现在视图上。

        RadioButton radioButton = (RadioButton) findViewById(R.id.your_radio_bt_id);
        

        【讨论】:

          【解决方案4】:

          this answer here 中所述,无法清除单个单选按钮。相反,您需要清除整个无线电组。

          所以不是

          radioButton.setChecked(false);
          

          你需要使用

          ((RadioGroup) findViewById(R.id.ID_OF_RADIO_GROUP)).clearCheck();
          

          此外,您还没有在问题中指定究竟是什么没有按预期工作。 如果这不能解决您的问题,请评论您在尝试触发开关或单选按钮时究竟得到了什么。

          编辑:另外,您应该使用setOnCheckedChangeListener() 而不是setOnClickListener()

          【讨论】:

          • 我不使用 RadioGroup ,我使用包含 RadioButton 的 RecyclerView ,在适配器中我将选中的 RadioButton 保存在实例变量中,当我检查另一个 RadioButton 时,之前保存的 RadioButton 将是未选中,新的将取而代之....
          • 我希望当我选中 Switch 按钮时 RadioButton 将被取消选中,反之亦然
          • @vaniokmd 请按照其他答案的建议尝试使用setOnCheckedChangeListener()
          猜你喜欢
          • 1970-01-01
          • 2013-01-08
          • 1970-01-01
          • 1970-01-01
          • 2011-12-29
          • 2020-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多