可以将 TextView 放在 RadioGroup 中.但是,这是好的做法吗? [英] It's possible to put a TextView inside of a RadioGroup. But, is it good practice?

查看:21
本文介绍了可以将 TextView 放在 RadioGroup 中.但是,这是好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What I'm trying to accomplish: When the user clicks a specific RadioButton, a TextView should appear immediately below the selected RadioButton.

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

My solution so far: Code-wise, put a TextView inside of the RadioGroup and set it's initial visibility to "invisible". Then, when a specific RadioButton is clicked, set the hidden TextView's visibility to "visible". When the RadioButton is unselected, hide the TextView. Setting the TextView's visiblity is done within an Activity class I've defined.

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

So, in my example XML code below, when "radio_button_one" is selected, "my_sometimes_hidden_textview" should appear. Conversely, when "radio_button_one" is deselected (or not selected), "my_sometimes_hidden_textview" should have it's visibility set to "invisible".

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

Question: Is putting the TextView inside of the RadioGroup valid (or, good practice)? If not, is there a better way to do what I'm trying to accomplish? I'm relatively new to Android development, so if there is something in the official Android documentation that I missed please point me to it. Thanks for the insight in advance.

<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.


Thank you for the replies, CommonsWare and Macarse.

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

CommonsWare: Thanks for clarifying that a RadioGroup is a type of LinearLayout. My instincts fought against adding the TextView because most RadioGroup examples I've come across to date don't show elements other than RadioButtons present. Hence, I thought that only RadioButtons should go in RadioGroups.

Macarse:

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

  1. I tried placing the TextView outside of the RadioGroup, just as you described. Your solution does work, so long as a programmer doesn't need the TextArea to appear immediately below a specific RadioButton. I haven't been able to figure out how to position the TextArea immediately below the specific RadioButton without explicitly placing the TextView inside of the RadioGroup.

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

Thanks for the ViewStub link. In fact, this may be the best way to accomplish what I'm trying to do overall. Along with the TextView I talked about in my question, I also wanted to add a button to appear next to the TextView only when a specific RadioButton was selected.

推荐答案

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

Is putting the TextView inside of the RadioGroup valid (or, good practice)?

RadioGroup 只是一个 LinearLayout 恰好也处理 RadioButton 排除规则(即,只能有一个...选中, 那是").我认为在 RadioGroup 中有一个 TextView 没有特别的问题.

RadioGroup is just a LinearLayout that happens to also handle RadioButton exclusion rules (i.e., "there can only be one...checked, that is"). I see no particular problems in having a TextView inside a RadioGroup.

这篇关于可以将 TextView 放在 RadioGroup 中.但是,这是好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆