android onCheckedChanged for radiogroup

走远了吗. 提交于 2019-12-03 23:19:46

问题


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
            }
        }
    });

回答1:


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);

Good Luck




回答2:


 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (checkedId == R.id.radioButton1) {
             //do work when radioButton1 is active
            } else  if (checkedId == R.id.radioButton2) {
             //do work when radioButton2 is active
          } else  if (checkedId == R.id.radioButton3) {
             //do work when radioButton3 is active
          }

        }
    });

this work for me . hope is helpfull




回答3:


Try this Code.

Declare this code in your onCreate method.

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
    radioGroup.clearCheck();
    radioGroup.setOnCheckedChangeListener(new  RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            if (null != rb && checkedId > -1) {

                // checkedId is the RadioButton selected
                switch (checkedId) {
                    case R.id.one:
                     // Do Something
                      break;

                    case R.id.two:
                    // Do Something
                      break;   

                    case R.id.three:
                    // Do Something   
                        break;
                }
            }
        }

    });

Hope this helps.




回答4:


The problem is because, you are setting OnCheckedChangeListener on RadioGroup after the providerRadio.performClick(); method call.

That's why when providerRadio.performClick(); method executes till then their is no OnCheckedChangeListener available to call. So, to make it work, you need to set Listener before the call to performClick() method.

And instead of using RadioButton.performClick() you should use RadioGroup.check() method to change the current checked RadioButton.

So you can use this code to change current selection of RadioGroup

RadioGroup ItemtypeGroup = (RadioGroup) findViewById(R.id.rechargeItemtype);
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
                }

            }

        });
ItemtypeGroup.check(R.id.a);

Note:- Make sure to call RadioGroup.check() method after adding OnCheckedChangeListener as above in code snippet.




回答5:


I guess you are performing click before setting listener to the group, that's why event is not triggered.



来源:https://stackoverflow.com/questions/18536195/android-oncheckedchanged-for-radiogroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!