gpt4 book ai didi

android - 如果已在 RadioGroup 中选中,如何取消选中 RadioButton

转载 作者:行者123 更新时间:2023-11-29 15:01:43 25 4
gpt4 key购买 nike

我在 RadioGroup 中有两个 RadioButton,最初都未选中。如果已经选中,我想取消选中 RadioButton。我试过了,但我做不到。当我选择任何单选按钮时,它不会将状态更改为第一次选中,如果我第二次选择相同的单选按钮,那么它正在更改其状态,如果我选择其他单选按钮,如果一个按钮被选中,那么它也不会改变状态第一次和所有按钮都未选中任何人都可以帮助我吗?我的代码如下...

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
pus="";

boolean isChecked = checkedRadioButton.isChecked();
if(isChecked){
checkedRadioButton.setChecked(false);

}else{
checkedRadioButton.setChecked(true);

}
}
Toast.makeText(context, pus, Toast.LENGTH_SHORT).show();
}
});
<RadioGroup
android:id="@+id/rdbt_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"

android:gravity="center"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/radio_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/rbtn_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:clickable="true"
android:textColor="@drawable/rbtn_textcolor_selector"
android:textSize="15sp" />

<RadioButton
android:id="@+id/radio_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:clickable="true"
android:textSize="15sp"
android:background="@drawable/rbtn_selector"
android:textColor="@drawable/rbtn_textcolor_selector"
android:padding="5dp"
android:gravity="center"
/>
</RadioGroup>

最佳答案

我知道现在回答这个问题有点晚了,但如果它能帮助别人,我会很高兴。

只需创建一个扩展RadioButtonAppCompatRadioButton 的自定义类并覆盖toggle() 方法即可。

public class UncheckableRadioButton extends AppCompatRadioButton {

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

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

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

@Override
public void toggle() {

if (isChecked()) {
if (getParent() != null && getParent() instanceof RadioGroup) {
((RadioGroup) getParent()).clearCheck();
}
} else {
super.toggle();
}
}

@Override
public CharSequence getAccessibilityClassName() {
return UncheckableRadioButton.class.getName();
}
}

如果您检查 radioGroup.getCheckedRadioButtonId(),这也会起作用。

关于android - 如果已在 RadioGroup 中选中,如何取消选中 RadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com