为了账号安全,请及时绑定邮箱和手机立即绑定

是否可以从 RadioGroup 中的 RadioButton 获取文本

是否可以从 RadioGroup 中的 RadioButton 获取文本

慕姐4208626 2023-03-09 14:54:43
对于 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 数组


查看完整回答
反对 回复 2023-03-09
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

我认为这不可能。为什么不为每个单选按钮提供 id 呢?



查看完整回答
反对 回复 2023-03-09
  • 2 回答
  • 0 关注
  • 269 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号