首页 > 解决方案 > RadioGroup ,再次单击相同的元素不起作用

问题描述

我试图在这个演示示例中使用 RadioGroup

<RadioGroup
        android:id="@+id/hello_world"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">

        <RadioButton
            android:id="@+id/checkbox_meat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""

            tools:ignore="OnClick" />

        <RadioButton
            android:id="@+id/checkbox_cheese"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </RadioGroup>

        <Button
            android:id="@+id/button1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="button1" />

和这样的Java代码:

firstCB.setText(firstCheckArray[i]);
secondCB.setText(secondCheckArray[i]);
button1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
        if (i<newStringArray.length-1){
        i++;
        firstCB.setText(firstCheckArray[i]);
        secondCB.setText(secondCheckArray[i]);

   }
  }
  });

我面临的问题是当我增加(1)并且下一个单词组来时,如果我想选择相同的单选按钮,直到我选择另一个单选按钮才选择它,那么我可以选择相同的旧单选按钮(换句话说,如果我选择第一个选择,在下一步中,第一个选择不可选择,直到我选择另一个,然后我可以回到第一个选择

标签: javaandroidxml

解决方案


You can set Radio button check by code in button1 click, like this

radioButton.setChecked(true);

And in your layout add

android:checked="true"

It will check when show

 <RadioButton
            android:id="@+id/checkbox_meat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:checked="true"
            tools:ignore="OnClick" />

推荐阅读