Android get value of the selected radio button

2019-01-11 12:58发布

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 by using:

if(rg1.getCheckedRadioButtonId()!=-1)
int id= rg1.getCheckedRadioButtonId()

that gives me the id , but I want the value of that button.

8条回答
三岁会撩人
2楼-- · 2019-01-11 13:09

One Line Code

String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
查看更多
▲ chillily
3楼-- · 2019-01-11 13:11

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();
}
查看更多
▲ chillily
4楼-- · 2019-01-11 13:13

SImple answer one line

View v = yourView;  // as a button

String radiovalue = (RadioButton)v).getText().toString();
查看更多
小情绪 Triste *
5楼-- · 2019-01-11 13:17
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();
查看更多
\"骚年 ilove
6楼-- · 2019-01-11 13:18

try this:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-11 13:18

I think you should try this

RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
查看更多
登录 后发表回答