【发布时间】: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:
正如您所描述的,我尝试将 TextView 放在 RadioGroup 之外。您的解决方案确实有效,只要程序员不需要 TextArea 立即出现在特定 RadioButton 下方。如果没有明确地将 TextView 放置在 RadioGroup 中,我无法弄清楚如何将 TextArea 定位在特定 RadioButton 的正下方。
感谢 ViewStub 链接。事实上,这可能是完成我想要做的事情的最佳方式。除了我在问题中谈到的 TextView 之外,我还想添加一个按钮,仅在选择特定 RadioButton 时才会出现在 TextView 旁边。
【问题讨论】:
标签: android radio-button