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

对于 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>

是否可以只获取文本?


慕姐4208626
浏览 271回答 2
2回答

三国纷争

您可以使用以下代码访问 RadioGroup 中的所有视图:&nbsp; &nbsp; int count = radioGroup.getChildCount();&nbsp;&nbsp; &nbsp; for (int i=0;i<count;i++) {&nbsp; &nbsp; &nbsp; &nbsp; View radioButton = radioGroup.getChildAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if (radioButton instanceof RadioButton) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG,"text: "+ radioButton.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }参考:在 Android 中获取 RadioGroup 中的 RadioButtons 数组

慕哥9229398

我认为这不可能。为什么不为每个单选按钮提供 id 呢?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java