How to check which radio button of a radio group is selected? [ANDROID]

我的未来我决定 提交于 2019-12-04 14:10:53
vijay chhalotre

Its working perfactly

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);

int radioButtonID = radioGroup.getCheckedRadioButtonId();

RadioButton radioButton = (RadioButton) 

radioGroup.findViewById(radioButtonID);

String selectedtext = (String) radioButton.getText();
vijay chhalotre

Try to use this one its also working for me

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                int radioBtnID = rGroup.getCheckedRadioButtonId();

                View radioB = rGroup.findViewById(radioBtnID);

                int position = group.indexOfChild(radioB);
            }
        });

Use following code.

RadioGroup rg = your_radio_group;

// It will return currently selected radio button id from RadioGroup
int id = rg.getCheckedRadioButtonId();

if(id == your_one_radio_button_id){
 // First radio button is selected.
} else if(id == your_two_radio_button_id){
 // Second radio button is selected.
}
vijay chhalotre
 int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();

 View radioButton = radioButtonGroup.findViewById(radioButtonID);

 int idx = radioButtonGroup.indexOfChild(radioButton);

If the RadioGroup contains other Views (like a TextView etc..) then the indexOfChild() method will return wrong index. to get selected RadioButton Text on RadioGroup

 RadioButton r = (RadioButton)  radioButton .getChildAt(idx);

 String selectedtext = r.getText().toString();
                int id = radioGroup.getCheckedRadioButtonId(); 
                 if(id!=-1){
                    int radioButtonID = radioGroup.getCheckedRadioButtonId();
                    RadioButton radioButton = 
                        (RadioButton)radioGroup.findViewById(radioButtonID);

                    String selectedtext = (String) radioButton.getText();
                      Toast.makeText(ReportsAct.this, ""+selectedtext, 
                       Toast.LENGTH_SHORT).show();
                }
radioGroup = view.findViewById(R.id.choices);
 try {
       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
       {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = view.findViewById(checkedId);
                //selected index
                mctSelectedAnsIndex = checkedId;
                Log.e("mctSelectedAnsIndex",mctSelectedAnsIndex+"-"+checkedId);
                int j = radioGroup.getChildCount();
               //changing background of all radio buttons in group
                for (int i = 0; i < j; i++) {
 radioGroup.getChildAt(i).setBackgroundColor(getActivity().getApplicationContext().
 getResources().getColor(R.color.lgrey));
                }

//code for changing background of selected one                      

radioButton.setBackgroundColor(getActivity().getApplicationContext().
getResources().getColor(R.color.color2));

                if (mctSelectedAnsIndex == correctAnsChoice){
                    rightAnswers++;
                }else{
                    wrongAnswers++;
                }

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