How do I lock the selected Radiobutton?

Asked

Viewed 410 times

-1

I created a RadioGroup with 3 RadioButton internal, however I cannot fit a code that makes the user when selecting a RadioButton is prevented from selecting another.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="O que você coloca em uma torradeira?"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@android:color/black"
        android:textStyle="bold"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rgRespostas">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rbResposta1"
            android:text="Torrada"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rbResposta2"
            android:text="Bolo"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rbResposta3"
            android:text="Pão"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>

    </RadioGroup>
</LinearLayout>

2 answers

1

The setEnabled does not block the radiobutton. I managed to solve otherwise.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedID) {
            if (checkedID == R.id.rb1){
                for(int i = 0; i < radioGroup.getChildCount(); i++){
                    ((RadioButton)radioGroup.getChildAt(i)).setEnabled(false);
                }
            }
        }
    });
}

0

You can disable the other radios button radiobutton.setEnabled(false); If that’s not what you imagined!

  • And how do I do that? Type, I create a method in Activity and link to the Radiogroup id and create the following condition: If user click on rbResposta1, then disable the rbResposta2 and rbResposta3 option. Something like that?

  • 1

    It would be easier to disable Radiogroup.

  • Voce can use the Oncheckedchangelistener method and inside put public void onCheckedChanged(compoundbutton compoundButton, Boolean b) { rbResposta2.setEnabled(false);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.