【问题标题】:Android java - is it possible to get text from RadioButton in RadioGroup without each RadioButton having a defined id?Android java - 是否可以从 RadioGroup 中的 RadioButton 获取文本,而每个 RadioButton 都没有定义的 id?
【发布时间】:2019-11-16 16:30:45
【问题描述】:

对于 android 编程,我有一个 RadioGroup,其中包含一些 RadioButtons。 我只想从按钮中获取文本,而不需要获取 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>

是否可以只获取文本?

【问题讨论】:

    标签: java android xml text radio-button


    【解决方案1】:

    您可以使用以下代码访问 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());
            }
        }
    

    参考:Get the array of RadioButtons in a RadioGroup in Android

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我可以做到,但这需要更多的时间和工作。我想我可以复制粘贴所有通用 ID。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2014-04-04
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多