重置 Radio 组内的 Radio 按钮 [英] Reset a Radio button inside a Radio group
问题描述
我如何重置单选组内的单选按钮,如下图示例
How I reset a radio button inside a radio group like the image example below
我所有的单选组及其单选按钮都是以编程方式创建的.
All my radio groups and its radio buttons are created programmatically.
我尝试设置 OnClickListener 以在更改之前获取单选按钮值,但是没有帮助.
I tried to to set OnClickListener to get the radio button value before getting changed but, it didn't help.
我在下面发布了答案,请检查.
I posted the answer below, please check it.
推荐答案
我是这样做的:
它是用 C# 编写的,但我认为将其转换为 Java 很容易.
It is in C# but I think it is easy to convert it to Java.
我使用标签来知道此单选按钮之前是否检查过.
I used tag to know if this radio button checked before or not.
False => 未检查
False => It is not checked
真 => 已检查
radiobutton.Tag = false;
radiobutton.Click += SingleChoiceQuestionAlternativeClick;
那么:
private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
{
RadioButton questionAlternative = sender as RadioButton;
if (questionAlternative != null)
{
RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;
if (questionAlternatives != null)
{
if (questionAlternative.Tag.Equals(false))
{
for (int i = 0; i < questionAlternatives.ChildCount; i++)
{
RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;
if (childRadioButton != null)
{
childRadioButton.Tag = false;
}
}
questionAlternative.Tag = true;
}
else
{
questionAlternative.Tag = false;
questionAlternatives.ClearCheck();
}
}
}
}
这篇关于重置 Radio 组内的 Radio 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!