【问题标题】:It's possible to put a TextView inside of a RadioGroup. But, is it good practice?可以将 TextView 放在 RadioGroup 中。但是,这是好的做法吗?
【发布时间】:2011-03-14 14:38:50
【问题描述】:

我要完成的工作:当用户单击特定的 RadioButton 时,TextView 应立即出现在所选 RadioButton 的下方。

到目前为止我的解决方案: 在代码方面,在 RadioGroup 中放置一个 TextView 并将其初始可见性设置为“不可见”。然后,当单击特定的 RadioButton 时,将隐藏的 TextView 的可见性设置为“可见”。取消选中 RadioButton 时,隐藏 TextView。设置 TextView 的可见性是在我定义的 Activity 类中完成的。

所以,在下面的示例XML代码中,选中“radio_button_one”时,应显示“my_sometimes_hidden_​​textview”。相反,当取消选择(或未选择)“radio_button_one”时,“my_sometimes_hidden_​​textview”应将其可见性设置为“不可见”。

问题:将 TextView 放在 RadioGroup 内是否有效(或者,好的做法)?如果没有,有没有更好的方法来做我想要完成的事情?我对 Android 开发比较陌生,所以如果我错过了官方 Android 文档中的某些内容,请指出。感谢您提前提供见解。

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/my_always_visible_textview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="This textview is always visible" />
    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10px">
        <RadioButton
            android:id="@+id/radio_button_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Radio Button 1" />
        <TextView
            android:id="@+id/my_sometimes_hidden_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This should only appear when radio_button_one is selected" />
        <RadioButton
            android:id="@+id/radio_button_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Radio Button 2" />
    </RadioGroup>
</LinearLayout>

感谢您的回复,CommonsWare 和 Macarse。

CommonsWare:感谢您澄清 RadioGroup 是一种线性布局。我的直觉反对添加 TextView,因为迄今为止我遇到的大多数 RadioGroup 示例都没有显示除了 RadioButtons 之外的元素。因此,我认为只有 RadioButtons 应该放在 RadioGroups 中。

Macarse:

  1. 正如您所描述的,我尝试将 TextView 放在 RadioGroup 之外。您的解决方案确实有效,只要程序员不需要 TextArea 立即出现在特定 RadioButton 下方。如果没有明确地将 TextView 放置在 RadioGroup 中,我无法弄清楚如何将 TextArea 定位在特定 RadioButton 的正下方。

  2. 感谢 ViewStub 链接。事实上,这可能是完成我想要做的事情的最佳方式。除了我在问题中谈到的 TextView 之外,我还想添加一个按钮,仅在选择特定 RadioButton 时才会出现在 TextView 旁边。

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    是的,可以检查这个方法

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:orientation="horizontal">
    
        <RadioButton
            android:id="@+id/ifsc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ifsc"
            android:textAllCaps="true" />
        <Textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/appName"
            android:id="@+id/ifsc_info"
            android:baselineAlignBottom="true"
            android:layout_toEndOf="@+id/ifsc" />
    
    </RadioGroup>
    

    【讨论】:

      【解决方案2】:

      将 TextView 放入 RadioGroup 有效(或,良好做法)?

      RadioGroup 只是一个 LinearLayout 恰好也处理 RadioButton 排除规则(即,“只能有一个......检查,即”)。我认为在RadioGroup 中包含TextView 没有什么特别的问题。

      【讨论】:

        【解决方案3】:

        两种选择:

        1. 按照你说的做类似的事情,但它不需要在RadioGroup 内。当用户单击RadioButton 时,使TextView 可见。
        2. 也许你可以用ViewStub 做点什么。检查this链接。

        【讨论】:

          猜你喜欢
          • 2014-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多