【问题标题】:How to change location of Android xml source file with radio button?如何使用单选按钮更改 Android xml 源文件的位置?
【发布时间】:2017-06-17 02:53:00
【问题描述】:

我有 3 个这样的 xml 源文件:

XmlPullParser xmlR =  params[0].getXml(R.xml.code_1);
XmlPullParser xmlR =  params[0].getXml(R.xml.code_2);
XmlPullParser xmlR =  params[0].getXml(R.xml.code_3);

这是我的单选按钮:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/separator2"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <RadioButton android:id="@+id/code_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="code_1"
            android:onClick="onRadioButtonClicked"
            />

        <RadioButton
            android:id="@+id/code_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="code_2"
            android:onClick="onRadioButtonClicked"
             />

        <RadioButton
            android:id="@+id/code_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="onRadioButtonClicked"
            android:text="code_3" />

    </RelativeLayout>

如何使用单选按钮选择仅一个 xml 文件? 因此,当我选择其中一个单选按钮时,将使用 xml code_1 或 xml code_2 或 xml code_3

谢谢。

【问题讨论】:

  • 使用 RadioGroup 作为所有单选按钮的父视图,因此一次只能选择其中的一个
  • 你能用java代码解释一下吗..

标签: android xml radio-button


【解决方案1】:

首先,您需要在 RadioGroup[1] 布局中对单选按钮进行分组,然后您必须设置 OnCheckedChangeListener[2] 这样您就可以知道选择了哪个单选按钮时间。

RadioGroup.OnCheckedChangeListener(RadioGroup group, int checkedId)

checkedId 与选中的 RadioButton 相关,因此您可以基于它使用您的 xml。

[1] - https://developer.android.com/reference/android/widget/RadioGroup.html [2] - https://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

【讨论】:

    【解决方案2】:

    您需要以编程方式扩充您的 xml !!!只需这样做,它就会起作用!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2011-08-06
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多