【发布时间】:2015-02-17 12:29:22
【问题描述】:
我正在制作一个应用程序,我想在其中设计 2*2 单选按钮中的选项。为此,我正在做以下事情,但出现“android.widget.RelativeLayout cannot be cast to android.widget.RadioButton”错误
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</TableRow>
<TableRow>
<TextView android:id="@+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="100dp"
android:gravity="center|left"/>
</TableRow>
<TableRow>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/group"
android:layout_marginLeft="25dp"
android:orientation="horizontal"
android:layout_span="2"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn1"/>
<RadioButton android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn1" />
<RadioButton android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn2"
android:layout_toRightOf="@id/btn3"/>
</RelativeLayout>
</RadioGroup>
</TableRow>
<Button android:id="@+id/submitbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:visibility="gone"
android:layout_below="@id/group" />
<TextView android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/submitbtn"/>
</TableLayout>
请帮忙,我从早上就被困住了。
【问题讨论】:
-
RadioGroup 不能包含除 RadioButtons 以外的其他视图,您的方式是不可能的...
-
那么应该怎么做呢?我现在已经制作了应用程序逻辑,所以我无法单独创建单选按钮并关闭,我只能使用 RadioGroup
-
您的单选组应该只包含单选按钮而不是RelativeLayout。您可以改为在相对布局或线性布局中使用两个单选组。如果Linear布局的方向是水平的,那么RadioGroup的方向应该是Vertical。
-
@Skynet-如何合并这两个无线电组以使它们作为一个组工作?
-
这取决于您需要什么。您可以做两个单选组并检查选定的,或者,如果您只想选择这四个按钮中的一个,您可以将它们放置在没有单选组的情况下,然后通过单击其中一个取消选择所有其他按钮。
标签: android android-tablelayout android-relativelayout android-radiogroup