android onCheckedChanged为radiogroup [英] android onCheckedChanged for radiogroup

查看:117
本文介绍了android onCheckedChanged为radiogroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android中编写 Activity ,其中在RadioGroup下有两个单选按钮.默认情况下会选中其中之一.但是我无法在 onCreate 方法中触发该事件,因此我无法在其中执行某些操作.点击 onCheckedChanged 时运行正常.

I'm writing an Activity in android where I have two radio buttons under a RadioGroup. One of them is checked by default. But I can't trigger the event in onCreate method so that I can do something in that. The onCheckedChanged is running fine when clicked on.

RadioGroup ItemtypeGroup = (RadioGroup) findViewById(R.id.rechargeItemtype);
    RadioButton providerRadio = (RadioButton) findViewById(R.id.a);
    providerRadio.performClick();

    ItemtypeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged (RadioGroup group,int checkedId){

            Log.d("chk", "id" + checkedId);

            if (checkedId == R.id.a) {
                //some code
            } else if (checkedId == R.id.b) {
                //some code
            }
        }
    });

推荐答案

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

To fire a radio check box for the default when initializing. Set everything to unchecked with the clearCheck method, then set the handler, then set the default and your handler will fire.

itemtypeGroup.clearCheck();

然后像往常一样添加一个侦听器...

then same as usual add a listener...

itemtypeGroup
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.a) {
                    //some code
                } else if (checkedId == R.id.b) {
                    //some code
                }

            }

        });

然后选中默认的单选按钮,您的听众将被触发.

Then check the default radio button and your listener will fire.

rb = (RadioButton) view.findViewById(R.id.a);
rb.setChecked(true);

祝你好运

这篇关于android onCheckedChanged为radiogroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆