android onCheckedChanged 用于无线电组 [英] android onCheckedChanged for radiogroup

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

问题描述

我正在 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 用于无线电组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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