选定的单选按钮,Android开值 [英] Android get value of the selected radio button

查看:144
本文介绍了选定的单选按钮,Android开值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RadioGroup中皂苷Rg1 ,我想获得选定的单选按钮的值。

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

我知道,我可以用得到选定的单选按钮的 ID

I know that I can get the id of the selected radio button by using:

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

这是给我的ID,但我想这个按钮的值。

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

推荐答案

您需要获得的单选按钮索引,然后获取该按钮的文本值。试试下面这个code。

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

这篇关于选定的单选按钮,Android开值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆