简体   繁体   English

Android获取所选单选按钮的值

[英]Android get value of the selected radio button

I have a RadioGroup rg1 and I want to get the value of the selected radio button. 我有一个RadioGroup rg1 ,我想获得所选单选按钮的值。

I know that I can get the id of the selected radio button by using: 我知道我可以使用以下命令获取所选单选按钮的id

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

that gives me the id , but I want the value of that button. 这给了我id,但我想要那个按钮的值。

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();
}

try this: 尝试这个:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();  

一行代码

String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());

现在,您可以使用rb1.getText()来获取已检查的Radiobutton上的文本

I think you should try this 我想你应该试试这个

RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();

SImple answer one line 简单回答一行

View v = yourView;  // as a button

String radiovalue = (RadioButton)v).getText().toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM