同一组内的单选按钮,但两个按钮都可以被选中

4

我在同一个RadioGroup中有两个RadioButtons,但是我希望每个RadioButton旁边都有一个ImageButton(信息)。因此我的代码如下:

<RadioGroup>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
<RadioGroup/>

但现在我面临的问题相当简单,两个单选按钮都可以被选择。我只想让其中一个在任何时候都被选中。

请忽略任何拼写错误。必须从手机发布此帖子。


可能是https://dev59.com/62XWa4cB1Zd3GeqPLlHD#11168605 的重复。 - Ye Min Htut
6个回答

1

RadioGroupLinearLayout本身,并且仅支持RadioButton作为直接子项。你可以在这里看到这种行为。

你试图以这种方式做的事情是不可能的,因为你将你的RadioButton包装在一个LinearLayout中。

你有两个选择:要么复制代码并制作自己的RadioGroup
要么扩展 RadioButton并通过在RadioButton内部应用自定义布局来改变其外观


0

RadioGroup不允许任何ViewGroup作为其子项。您将需要使用drawableRight属性。


我猜你没有理解这个问题。我建议你再读一遍。 - Srujan Barai

0

我找到了一个适合我的解决方案,因为我只有很少的RadioButtons

我为每个RadioButton设置了一个OnClickListener(),并将其他未选中的设置为不可选。

final RadioButton a1 = (RadioButton) findViewById(R.id.a1);
final RadioButton a2 = (RadioButton) findViewById(R.id.a2);
    a1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a2.setChecked(false);
        }
    });
    a2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a1.setChecked(false);
        }
    });

我想我会使用相同的方法。但由于我有很多选项,我会跟踪最后选择的radioButton,并在OnClickListener()中实现lastRadioButtonSelected.setChecked(false)。我猜我必须强制使用Android SDK来按时完成工作! - Someone Somewhere

0

RadioButton rbOne, rbTwo, rbThree, rbFour;

单选按钮 rbOne、rbTwo、rbThree、rbFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rbOne = findViewById(R.id.rbOne);
    rbTwo = findViewById(R.id.rbTwo);
    rbThree = findViewById(R.id.rbThree);
    rbFour = findViewById(R.id.rbFour);

    rbOne.setOnClickListener(this);
    rbTwo.setOnClickListener(this);
    rbThree.setOnClickListener(this);
    rbFour.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == rbOne) {
        isChecked(1);
        Toast.makeText(getApplicationContext(), rbOne.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbTwo) {
        isChecked(2);
        Toast.makeText(getApplicationContext(), rbTwo.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbThree) {
        isChecked(3);
        Toast.makeText(getApplicationContext(), rbThree.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbFour) {
        isChecked(4);
        Toast.makeText(getApplicationContext(), rbFour.getText().toString(), Toast.LENGTH_SHORT).show();
    }
}

private void isChecked(int position) {
    switch (position) {
        case 1:
            rbTwo.setChecked(false);
            rbThree.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 2:
            rbOne.setChecked(false);
            rbThree.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 3:
            rbOne.setChecked(false);
            rbTwo.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 4:
            rbOne.setChecked(false);
            rbTwo.setChecked(false);
            rbThree.setChecked(false);
            break;
    }
}

<RadioGroup
    android:id="@+id/rgMemberShip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One Time"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rbTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:text="One Time"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One Time"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rbFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:text="One Time"
            android:textSize="16sp" />
    </LinearLayout>

</RadioGroup>

0
  1. 你只能将单选按钮用作单选组的子项
  2. 如果您不想这样做,可以使用我的代码并删除单选组,然后使用自定义单选按钮

      final RadioButton a1 = (RadioButton) findViewById(R.id.a1);
     final RadioButton a2 = (RadioButton) findViewById(R.id.a2);
     a1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          a2.setChecked(false);                    
          a1.setChecked(true);
    
      }
         });
       a2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        a1.setChecked(false);
        a2.setChecked(true);
    
    }
    });
    

-1

更新:我认为这会对你有所帮助。我曾经遇到过同样的问题。

<LinearLayout> //Horizontal
    <LinearLayout>//Vertical
        <ImageButton/>
        <ImageButton/>
    </LinearLayout>
    <LinearLayout>//Vertical
        <RadioGroup> //Vertical
            <RadioButton/>
            <RadioButton/>
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

1 [这是最终显示的方式]


我不确定这是否有帮助,因为您没有展示如何将一个“ImageButton”放在这些“RadioButtons”旁边。 - cldrr
你不需要<LinearLayout>//Vertical,只需使用RadioGroup替换它,因为RadioGroup已经是一个布局。然而,我想这种方法对齐单选按钮和图像可能很痛苦。 - Someone Somewhere
我重新更新了符合您需求的答案。您能否请重新考虑点赞?这样,当您单击单选按钮时,因为它们位于单选组下面,它将交换已单击的变量。 - Dimitri Leite

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接