对于 android 编程,我有一个包含一些 RadioButtons 的 RadioGroup。我只想从按钮中获取文本,而无需获取 RadioButton 的 ID。<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioGroup" > <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="text1" /> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="text2" /> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="text3" /> </RadioGroup>是否可以只获取文本?
2 回答
三国纷争
TA贡献1804条经验 获得超7个赞
您可以使用以下代码访问 RadioGroup 中的所有视图:
int count = radioGroup.getChildCount();
for (int i=0;i<count;i++) {
View radioButton = radioGroup.getChildAt(i);
if (radioButton instanceof RadioButton) {
Log.d(TAG,"text: "+ radioButton.getText().toString());
}
}
参考:在 Android 中获取 RadioGroup 中的 RadioButtons 数组
添加回答
举报
0/150
提交
取消
