gpt4 book ai didi

c# - 在 Xamarin.Android 中单击 RadioGroup 按钮时未选中 RadioButton

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:16 26 4
gpt4 key购买 nike

有时我的 RadioGroup 按钮在选择时没有被选中。我正在使用 Xamarin.Android 并且我在 RadioGroup 中有大约 15 个 RadioButtons。奇怪的是,焦点总是设置在点击的 RadioButton 上,但有时同一个 RadioButton 在点击后没有设置为 Checked。这是我目前使用的示例代码:

radioGroup.CheckedChange += delegate
{
var radioButton = FindViewById<RadioButton>(radioGroup.CheckedRadioButtonId);
radioButton.Focusable = true;
radioButton.FocusableInTouchMode = true;
radioButton.RequestFocus();
radioButton.Checked = true;
};

如何在每次选择 RadioButton 时将其标记为已选中?提前致谢。

最佳答案

What is strange is that the focus is always set on the RadioButton clicked, but sometimes the same RadioButton is not set as Checked after click.

因为当您点击 RadioButton 时 GetFocus 总是最先发生,因此当您第一次点击 RadioButton 时,CheckedChange 事件永远不会被触发。

因此,正确的做法是为每个 RadioButton 注册 focusChange 事件,并在 focusChange 中将单选按钮设置为选中事件处理程序:

public class MainActivity : Activity
{
RadioGroup radioGroup;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
radioGroup = FindViewById<RadioGroup>(Resource.Id.mGroup);

//register focus change event for every sub radio button.
for (int i = 0; i < radioGroup.ChildCount; i++)
{
var child=radioGroup.GetChildAt(i);
if (child is RadioButton)
{
((RadioButton)child).FocusChange += RadioButton_FocusChange;
}
}
}


private void RadioButton_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)
{
//check if the radio button is the button that getting focus
if (e.HasFocus){
((RadioButton)sender).Checked = true;
}
}
}

关于c# - 在 Xamarin.Android 中单击 RadioGroup 按钮时未选中 RadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654520/

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