How do I block the selected RadioButton?

-1

I've created a RadioGroup with 3% internal%, but I can not fit a code that causes the user to select a RadioButton to be prevented from selecting another one.

<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>
    
asked by anonymous 04.12.2017 / 18:24

2 answers

1

SetEnabled does not block the radioButton. I have managed to solve it in another way.

@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);
                }
            }
        }
    });
}
    
05.12.2017 / 17:36
0

You can disable the other radios button radioButton.setEnabled (false); If that's not what you just imagined to be!

    
04.12.2017 / 20:46