Android get value of the selected radio button

前端 未结 8 979
慢半拍i
慢半拍i 2020-12-03 06:53

I have a RadioGroup rg1 and I want to get the value of the selected radio button.

I know that I can get the id of the selected radio button

相关标签:
8条回答
  • 2020-12-03 07:48

    You need to get the radio button at that index, then get the value of the text of that button. Try this code below.

    if(rg1.getCheckedRadioButtonId()!=-1){
        int id= rg1.getCheckedRadioButtonId();
        View radioButton = rg1.findViewById(id);
        int radioId = radioGroup.indexOfChild(radioButton);
        RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
        String selection = (String) btn.getText();
    }
    
    0 讨论(0)
  • 2020-12-03 07:53

    try this:

    RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
    String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
    
    0 讨论(0)
提交回复
热议问题