我想要完成的任务:当用户点击特定的RadioButton时,TextView应该出现在所选RadioButton的正下方.
到目前为止我的解决方案:代码方面,将一个TextView放在RadioGroup中并将其初始可见性设置为"不可见".然后,当单击特定的RadioButton时,将隐藏的TextView的可见性设置为"可见".取消选择RadioButton时,隐藏TextView.设置TextView的可见性是在我定义的Activity类中完成的.
因此,在下面的示例XML代码中,当选择"radio_button_one"时,应出现"my_sometimes_hidden_textview".相反,当取消选择(或未选择)"radio_button_one"时,"my_sometimes_hidden_textview"应将其可见性设置为"不可见".
问题:将RadioView内的TextView置于有效(或者,良好做法)?如果没有,有没有更好的方法来做我想要完成的事情?我对Android开发相对较新,所以如果我错过了官方Android文档中的某些内容,请指出我.感谢您提前了解.
<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>
Run Code Online (Sandbox Code Playgroud)
感谢您的回复,CommonsWare和Macarse.
CommonsWare:感谢您澄清RadioGroup是一种LinearLayout.我的直觉反对添加TextView,因为我遇到的大多数RadioGroup示例都没有显示RadioButtons以外的元素.因此,我认为只有RadioButtons才能进入RadioGroups.
Macarse:
我尝试将TextView放在RadioGroup之外,就像你描述的那样.只要程序员不需要TextArea直接出现在特定的RadioButton下面,您的解决方案就可以正常工作.我无法弄清楚如何将TextArea放置在特定RadioButton的正下方,而无需将TextView放在RadioGroup中.
感谢ViewStub链接.事实上,这可能是我完成整体尝试的最好方法.除了我在问题中谈到的TextView之外,我还想在选择特定的RadioButton时添加一个按钮以显示在TextView旁边.