gpt4 book ai didi

android - 在 android 中,当我通过调用 setChecked(true) 以编程方式设置单选按钮时,不会触发单选组的 onCheckedChanged

转载 作者:行者123 更新时间:2023-11-29 14:53:16 25 4
gpt4 key购买 nike

在我的应用程序中,我需要以编程方式设置单选按钮并期望触发单选组的 onCheckedChanged 以便我可以在其中执行某些操作。

以编程方式设置单选按钮的代码。

 LayoutInflater inflater = LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.footer, null);
RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
btn.setChecked(true);

在另一个 Activity 中,我做了类似的事情:

 public class MainActivity extends ActivityGroup implements RadioGroup.OnCheckedChangeListener{
protected void onCreate(Bundle savedInstanceState){
mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
mRadioGroup.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
RadioButton btn = (RadioButton)findViewById(checkedId);
if(btn != null){
do_something();
}
}
}

但是,看起来这种方式不起作用,从未调用过 onCheckedChanged。任何人都知道我应该做什么?谢谢。

最佳答案

在初始化时触发默认的单选复选框。使用 clearCheck 方法将所有内容设置为未选中。然后,设置处理程序,最后设置默认值,您的处理程序将触发。

itemtypeGroup.clearCheck();

然后,像往常一样添加一个监听器:

mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
mRadioGroup.setOnCheckedChangeListener(this);
mRadioGroup.clearCheck();

RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
btn.setChecked(true);

关于android - 在 android 中,当我通过调用 setChecked(true) 以编程方式设置单选按钮时,不会触发单选组的 onCheckedChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12310818/

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