这是可能把一个TextView一个RadioGroup中的内部。但是,它是很好的做法? [英] It's possible to put a TextView inside of a RadioGroup. But, is it good practice?

查看:222
本文介绍了这是可能把一个TextView一个RadioGroup中的内部。但是,它是很好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到::当用户点击一个特定的单选按钮,一个TextView应该立即出现在选中单选按钮下方

到目前为止,我的解决办法: code,明智的,把一个TextView的RadioGroup中的内部,设置它的初始可见性,以隐形。然后,点击一个特定的单选按钮时,设置隐藏的TextView的可见性可见的。当单选按钮为未选中状态,隐藏的TextView。设置TextView中的可见性是我所定义的活动类别内完成。

所以,在我的示例XML code以下,当选择radio_button_one,my_sometimes_hidden_​​textview应该会出现。相反,当radio_button_one被取消(或不选择),my_sometimes_hidden_​​textview应该有它的可见性设置为隐身。

问:是放的TextView的RadioGroup中有效(或好的做法)内?如果没有,是否有更好的方法做什么,我试图完成?我是比较新的Andr​​oid开发,所以如果有东西,我错过了请点我给它的官方Android文档中获得。感谢提前洞察力。

main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:背景=#FFFFFF
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <的TextView
        机器人:ID =@ + ID / my_always_visible_textview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=这TextView中始终可见/>
    < RadioGroup中
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:填充=10px的>
        <单选
            机器人:ID =@ + ID / radio_button_one
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=单选按钮1/>
        <的TextView
            机器人:ID =@ + ID / my_sometimes_hidden_​​textview
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字= /&gt的帮助时radio_button_one选择这应该只出现
        <单选
            机器人:ID =@ + ID / radio_button_two
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=单选按钮2/>
    < / RadioGroup中>
< / LinearLayout中>


感谢您的答复,CommonsWare和Macarse。

CommonsWare:感谢澄清一个RadioGroup中是一种类型的LinearLayout的。我的直觉反对加入,因为大部分RadioGroup中的例子,我遇到迄今没有显示出比单选按钮present其他元素TextView的战斗。因此,我以为只有单选按钮应该RadioGroups走了。

Macarse:


  1. 我试过把TextView的的RadioGroup中之外,就像你描述。您的解决方案没有工作,只要程序员不需要文本区域出现立即低于特定单选按钮。我一直无法弄清楚如何将文本区立即放置特定的单选按钮下方没有明确放置的TextView的RadioGroup中的内部。


  2. 感谢您的ViewStub链接。事实上,这可能是完成我想要做整体的最佳方式。随着我谈到了我的问题TextView的,我也想加入一个按钮旁边只有当选择了特定的单选按钮TextView的出现。



解决方案

  

时把TextView的内部
  RadioGroup中有效(或好的做法)?


RadioGroup中只是一个的LinearLayout 碰巧还处理单选排除规则(即有可能只有一个...经过,那就是)。我看到在没有特殊问题的TextView RadioGroup中

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

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.

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

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.

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>


Thank you for the replies, CommonsWare and Macarse.

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

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

解决方案

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

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天全站免登陆