在Android中,当我通过编程调用设置一个单选按钮没有被触发一个RadioGroup中的onCheckedChanged setChecked(真) [英] In android, onCheckedChanged of a Radiogroup is not triggered when I Programmatically set a Radiobutton by call setChecked(true)
问题描述
在我的应用程序,我需要以编程方式设置一个单选按钮,并期待着radioGroup中的onCheckedChanged被触发,这样我可以在里面做一些事情。
In my application, I need to programmatically set a radiobutton and expect the onCheckedChanged of a Radiogroup to be triggered so that I can do something in it.
code编程设置一个单选按钮。
Code to set a radiobutton programmatically.
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);
而在另一个活动,我不喜欢的东西:
And in another activity, I did something like:
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从未被调用。任何人都知道我应该怎么办呢?谢谢你。
However, looks like this way doesn't work, the onCheckedChanged was never called. Anyone know what I should do? Thanks.
推荐答案
我不知道对于这个问题,但它可能是你错过参考收音机组ID。
试试这个,看看效果(内你的的onCreate()
)
I am not sure for the issue, but it may be you miss refer your radio group id.
Try this instead to see the effect (inside your onCreate()
):
RadioGroup rdGroup = (RadioGroup) findViewById(R.id.footer_radio);
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton btn = (RadioButton)findViewById(checkedId);
if(btn != null){
do_something();
}
}
});
,也可以是(单选)findViewById(checkedId)
返回null。
尝试微调
如果你的情况变得模棱两可。
Try Spinner
if your case gets ambiguous.
这篇关于在Android中,当我通过编程调用设置一个单选按钮没有被触发一个RadioGroup中的onCheckedChanged setChecked(真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!