Android获取所选单选按钮的值

时间:2012-06-25 17:46:16

标签: android android-event

我有一个RadioGroup rg1,我想获得所选单选按钮的值。

我知道我可以使用以下方法获取所选单选按钮的id

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

给了我id,但我想要那个按钮的值。

8 个答案:

答案 0 :(得分:62)

您需要获取该索引处的单选按钮,然后获取该按钮文本的值。请尝试以下代码。

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

答案 1 :(得分:50)

试试这个:

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

答案 2 :(得分:3)

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

答案 3 :(得分:1)

一行代码

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

答案 4 :(得分:1)

rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());

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

答案 5 :(得分:0)

我认为你应该试试这个

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

简单回答一行

View v = yourView;  // as a button

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