所选择的单选按钮的Android开值(Android get value of the select

2019-06-25 18:22发布

我有一个RadioGroup中rg1 ,我想选择的单选按钮的值。

我知道,我能得到id使用选择的单选按钮:

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

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

Answer 1:

你需要一个索引处得到的单选按钮,然后得到那个按钮上的文字的值。 下面试试这个代码。

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


Answer 2:

试试这个:

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


Answer 3:

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


Answer 4:

一号线码

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


Answer 5:

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

现在你可以使用rb1.getText()来获取被选中的单选框文本



Answer 6:

我想你应该试试这个

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


Answer 7:

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


Answer 8:

简单的答案一行

View v = yourView;  // as a button

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


文章来源: Android get value of the selected radio button