我有一个名为rg1
的RadioGroup,我想获取所选单选按钮的值。
我知道可以使用以下代码获取所选单选按钮的id
:
if(rg1.getCheckedRadioButtonId()!=-1)
int id= rg1.getCheckedRadioButtonId()
这会给我按钮的id,但我想要该按钮的值。
我有一个名为rg1
的RadioGroup,我想获取所选单选按钮的值。
我知道可以使用以下代码获取所选单选按钮的id
:
if(rg1.getCheckedRadioButtonId()!=-1)
int id= rg1.getCheckedRadioButtonId()
这会给我按钮的id,但我想要该按钮的值。
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();
}
试一下这个:
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上的文本。我认为你应该尝试这个
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();
View v = yourView; // as a button
String radiovalue = (RadioButton)v).getText().toString();
使用这段Kotlin代码,在单选按钮组中仅在单选按钮被选中时获取单选按钮文本 -
radioGroup.setOnCheckedChangeListener { rg, i ->
val selectedId = radioGroup.checkedRadioButtonId
val radioButton = findViewById<RadioButton>(selectedId)
myTextView.text = radioButton.text
}
isChecked
方法获取其文本(如果已选中)。像这样:RadioButton radio_male = ((RadioButton) findViewById(R.id.rbtn_male)); String gend = radio_male.isChecked() ? radio_male.getText().toString() : radio_female.getText().toString();
- vikas devde(RadioButton) rg1.findViewById(id).getText();
? - Aman Grover