android onCheckedChanged for radiogroup

前端 未结 6 1700
死守一世寂寞
死守一世寂寞 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 17:34

    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.

提交回复
热议问题