【问题标题】:how to arrange 2*2 radiogroup如何安排2*2的radiogroup
【发布时间】: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


【解决方案1】:

首先,这取决于您的需求。但你可以这样做:Juste 不要使用 RadioGroup,而是使用两个 LinearLayout:

  <LinearLayoutLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <RadioButton android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

    <RadioButton android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  </LinearLayoutLayout>

 <LinearLayoutLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <RadioButton android:id="@+id/btn3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"  />

    <RadioButton android:id="@+id/btn4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayoutLayout>

然后您可以通过编程方式引用它并取消选择所有其他按钮(如果按下):

    yourFirstRadioButton.setOnClickListener(new OnClickListener(){

      @Override
      public void onClick(View view){

       YourSecondRadioButton.setChecked(false);
       YourThirdRadiobutton.setChecked(false);
       YourFourthRadioButton.setChecked(false);

      }

    });

您必须为您的所有单选按钮执行此操作。这只是一个简单的例子,你可以用更好的方式来做,例如使用 for 循环。例如,创建 RadioButtons 后,初始化一个 RadioButtons Array:

 private RadioButton[] radioButtons;

初始化所有radioButtons后初始化:

 radioButtons = {radioButton1,radioButton2,radioButton3,radioButton4};

然后,制作一个 setSelected(RadioButton button) 方法:

   private void setSelected(RadioButton button){

   for(int i=0;i<radioButtons.length;i++){

      if(radioButtons[i]!=button){

          radioButtons[i].setChecked(false);
      }
    }

   }

并在你的 onClick() 中调用它:

  yourFirstRadioButton.setOnClickListener(new OnClickListener(){

      @Override
      public void onClick(View view){

       setSelected(yourFirstRadioButton);

      }

    });

这可能是我的代码有些小错误,因为我目前无法测试它,这里没有 IDE。但是,请尝试一下,如果它不起作用,请返回。

【讨论】:

    【解决方案2】:

    1.删除无线电组。

    2.以父布局为相对

    3.根据需要取4个单选按钮

    4.为所有单选按钮编写onclicklistener

    5.在单选按钮 1 onclicklistener 事件中禁用其他按钮并为其他按钮执行此操作(设置检查特定按钮和未选中其他设置)

    【讨论】:

      【解决方案3】:

      删除 Radio Group,为 RadioButtons 实现 onClickListener 并保持活动的 Radio Button Id。喜欢:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      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"
          android:onClick="onClickButton" />
      
      <RadioButton
          android:id="@+id/btn2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@id/btn1"
          android:onClick="onClickButton" />
      
      <RadioButton
          android:id="@+id/btn3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/btn1"
          android:onClick="onClickButton" />
      
      <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"
          android:onClick="onClickButton" />
      
      </RelativeLayout> 
      

      然后,在你的activity中实现了RadioButtons的OnClick属性中提到的“onClickButton”方法:

      private RadioButton checkedRadioButton;
      
      // OnClick listener for Radio Buttons
      public void onClickButton(View view) {
          RadioButton radioButton = (RadioButton) view;
          if (checkedRadioButton != null) {
              checkedRadioButton.setChecked(false);
          }
          radioButton.setChecked(true);
          checkedRadioButton = radioButton;
      }
      
      // Get the Active radio button ID
      private int getCheckedRadiButtonId() {
         if (checkedRadioButton != null) {
              return checkedRadioButton.getId();
          }
          return 0;
      }
      

      U 可以使用 getCheckedRadiButtonId 获取当前活动的 radioButton Id。

      【讨论】:

      • 如果我使用这个解决方案,我已经完成了应用程序,那么我必须从 strach 编码它,这无论如何都非常忙碌,感谢您的帮助欢​​呼!!
      猜你喜欢
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 2022-01-13
      • 2017-06-27
      相关资源
      最近更新 更多