android onCheckedChanged for radiogroup

前端 未结 6 1737
死守一世寂寞
死守一世寂寞 2021-01-07 16:39

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

6条回答
  •  长情又很酷
    2021-01-07 17:36

    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

提交回复
热议问题