Android 获取所选单选按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11194515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android get value of the selected radio button
提问by Totti
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 ,但我想要那个按钮的值。
回答by Otra
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();
}
回答by ρяσ?ρ?я K
try this:
尝试这个:
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();
回答by prawins
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
回答by karthik gorijavolu
rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());
Now you can use rb1.getText()
to get the text on the Radiobutton that is checked
现在您可以使用rb1.getText()
获取选中的 Radiobutton 上的文本
回答by Dhruvil Shah
One Line Code
一行代码
String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
回答by vincent
SImple answer one line
简单回答一行
View v = yourView; // as a button
String radiovalue = (RadioButton)v).getText().toString();
回答by Faruk Nasir
I think you should try this
我想你应该试试这个
RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
回答by Ganesh Jogam
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();