Issue
I have a RadioButton with name radiobutton
. I want to use it as Check Box. But once checked you cannot uncheck it by clicking. So I added this code:
radiobutton.Click += (s, e) =>
{
if (radiobutton.Checked is false)
radiobutton.Checked = true;
else
radiobutton.Checked = false;
};
However, it does not work. With this code I cannot check the RadioButton. What's wrong with my code?
Solution
It will be an expected result in your case . When we click the RadioButton , the value of Checked will been changed automatically .So if you set it manually again it will be in a endless loop .
By the way , RadioButton always be used to selected one value between multi options . So the best way to solve the issue is to use CheckBox instead of RadioButton .
If you do want to use the style of RadioButton , you could put the RadioButton in a RadioGroup . And invoke the method ClearCheck()
to uncheck the button.
in xml
<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check" />
</RadioGroup>
in Activity
bool CurrentStatus = false;
private void Radio_Click(object sender, EventArgs e)
{
RadioButton radio = FindViewById<RadioButton>(Resource.Id.radio);
bool isChecked = radio.Checked;
if (isChecked && CurrentStatus)
{
RadioGroup group = FindViewById<RadioGroup>(Resource.Id.group);
group.ClearCheck();
CurrentStatus = false;
}
else
{
CurrentStatus = isChecked;
}
}
Answered By - Lucas Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.