【问题标题】:Android radio button uncheckAndroid单选按钮取消选中
【发布时间】:2023-03-29 18:43:01
【问题描述】:

该应用程序是一个步进音序器应用程序,具有 16 个无线电组,每组有 8 个按钮。它工作得很好,除非一个组选择了一个按钮,除非我使用我创建的清除按钮来清除所有无线电组,否则我无法将其关闭。我想添加的是一些代码,当再次选择选定的单选按钮时,它会像切换按钮一样关闭。我尝试使用切换,但该方法出现了其他问题。以下是两次尝试,但都只是阻止我使用按钮

final RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.RadioGroup1);
RadioButton D1 = (RadioButton) findViewById(R.id.RadioButtonD1);

Button D1 = (Button) findViewById(R.id.RadioButtonD1);
D1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick (View v){
        PdBase.sendFloat("D1", 74);
        int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
        RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
        if (D1 != null) // This will be null if none of the radio buttons are selected
            radioGroup1.clearCheck();
        PdBase.sendFloat("D1", 0);
    }
});

RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1);
lC1.setOnClickListener(new View.OnClickListener() {
    public void onClick (View v) {
        int selectedTypeId = radioGroup1.getCheckedRadioButtonId();

        RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1);
        if (selectedTypeId == -1) {
            PdBase.sendFloat("lC1", 72);
        }
        else if (selectedTypeId == R.id.RadioButtonlowC1) {
            radioGroup1.clearCheck();
            PdBase.sendFloat("lC1", 0);
        }
    }
});

【问题讨论】:

  • 为什么不给每个组添加一个OFF按钮?
  • RadioButtons 通常不是这样工作的,也不是人们所期望的。您应该将RadioButton 像none 添加到您的组中
  • 我可以这样做,但由于它是单声道的,我希望每个组中只选择一个
  • 为每个组设置一个清晰的按钮会起作用,但不是很实用
  • 无论如何,您只会在每个组中选择一个。这就是单选按钮的工作方式。从设计的角度来看,您的问题是您实际上没有使用任何按钮作为额外条件,您无法切换。

标签: android button radio


【解决方案1】:

我最近有同样的需求 - 有一个单选组,可以通过再次点击它来取消选择所选项目。我发现我无法使用侦听器来实现这一点,但我能够使用自定义 RadioButton 来实现它,就像这样......

public class ToggleableRadioButton extends RadioButton {
    // Implement necessary constructors

    @Override
    public void toggle() {
        if(isChecked()) {
            if(getParent() instanceof RadioGroup) {
                ((RadioGroup)getParent()).clearCheck();
            }
        } else {
            setChecked(true);
        }
    }
}

请注意,按钮根据其当前状态以不同方式切换 - 即,在按钮上调用 setChecked(true) 与在组上调用 clearCheck()。如果在这两种情况下都使用setChecked(),则无法立即重新选择刚刚取消选择的按钮 - RadioGroup 中的逻辑似乎会立即取消选择它。

要使用此按钮,只需在布局 XML 中将 <RadioButton> 标记替换为 <your.package.ToggleableRadioButton>。

【讨论】:

  • 在实现构造函数时出现错误 LOGCAT:- ToggleableRadioButton. [class android.content.Context, interface android.util.AttributeSet]
【解决方案2】:

我刚刚使用了@spaaarky21的答案

我的完整代码看起来像这样,并且运行良好!

Java 类

public class ToggleableRadioButton extends RadioButton {


    public ToggleableRadioButton(Context context) {
        super(context);
    }

    public ToggleableRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void toggle() {
        if(isChecked()) {
            if(getParent() instanceof RadioGroup) {
                ((RadioGroup)getParent()).clearCheck();
            }
        } else {
            setChecked(true);
        }
    }
}

对于 XML 布局

<com.smart_dent.adapters.ToggleableRadioButton android:id="@+id/tejido_blando_perfil_convexo"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:text="@string/tejido_blando_convexo_label" />

在这种情况下你只需要更改包,我这个很容易找到,它就在 Java Class Flie 的顶部(如果你是从 Android Studio 创建的)

【讨论】:

    【解决方案3】:

    实际上可以通过监听器来完成,但使用OnTouchListener,它将在按钮状态更改之前触发,而不是通常的OnClickListener。以下对我有用:

    View.OnTouchListener radioButtonOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (((RadioButton) v).isChecked()) {
                // If the button was already checked, uncheck them all
                radioGroup.clearCheck();
                // Prevent the system from re-checking it
                return true;
            }
            return false;
        }
    };
    radioButton1.setOnTouchListener(radioButtonOnTouchListener);
    radioButton2.setOnTouchListener(radioButtonOnTouchListener);
    

    其中radioGroup 是radioButton1 和radioButton2 的父级

    【讨论】:

    • onTouch() 是相当低的级别。似乎这会取消选择除点击/单击以外的手势按钮。
    • 嗯,好点子,@spaarky21。我想我们还应该检查事件的类型是否为ACTION_DOWN。虽然,对此进行测试,我无法触发任何看似异常的东西
    【解决方案4】:

    从@spaarky21 回答编辑

    @Override
    public void toggle() {
        if (isChecked()) {
            if (getParent() instanceof RadioGroup) {
                ((RadioGroup) getParent()).clearCheck();
            } 
            // add else here when a single radioButton without radioGroup
            else {
                setChecked(false);
            }
        } else {
            setChecked(true);
        }
    }
    

    【讨论】:

      【解决方案5】:

      这也能起到作用:

      public final class ToggleAbleRadioButton extends AppCompatRadioButton {
        public ToggleAbleRadioButton(final Context context, final AttributeSet attrs) {
          super(context, attrs);
        }
      
        @Override public void toggle() {
          setChecked(!isChecked());
        }
      }
      

      【讨论】:

      • 不起作用,如果您检查两次,则不会再次检查,因为 appCompatRadioButoon 中的代码 - if (mBroadcasting) { return; }
      【解决方案6】:

      您可以使用布尔值来打开和关闭按钮。

      在代码中的某处添加一个布尔值:

      var radioButton1IsSelected = false
      

      然后为按钮设置 onClickListener:

      radioButton1.setOnClickListener {
              radioButton1IsSelected = !radioButton1IsSelected
              radioButton1.isChecked = radioButton1IsSelected
          }
      

      【讨论】:

        猜你喜欢
        • 2011-10-14
        • 1970-01-01
        • 2013-10-14
        • 2012-01-23
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 2012-04-30
        相关资源
        最近更新 更多