Home > Back-end >  How to disable edit text after clicking radio button
How to disable edit text after clicking radio button

Time:10-22

here is my xml file grouped by radiogroup

    <RadioGroup

        <TextView
            android:text="Religion"/>

        <RadioButton
            android:id="@ id/islam"
            android:text="Islam" />

        <RadioButton
            android:id="@ id/christian"
            android:text="Christian" />

        <EditText
            android:id="@ id/txtchristian"
            android:hint="pls specify"
            android:inputType="text" />

        <RadioButton
            android:id="@ id/others"
            android:text="Others" />

        <EditText
            android:id="@ id/txtothers"
            android:hint="pls specify"
            android:inputType="text" />
    </RadioGroup>

what I am trying to do is to disable the edit text if they are not selected. e.g. if Islam is selected, those two edit text must be disabled.

  if(islam.isChecked()){
        txtothers.setEnabled(false);
        txtchristian.setEnabled(false);
    }
    else if(christian.isChecked()){
        txtothers.setEnabled(false);
        txtchristian.setEnabled(true);
    }
    else{
        txtothers.setEnabled(true);
        txtchristian.setEnabled(false);
    }

choose religion using radio button

CodePudding user response:

group.setOnCheckedChangeListener((a, b) -> {
       txtothers.setEnabled(!islam.isChecked() && !christian.isChecked);
       txtchristian.setEnabled(christian.isChecked());
});
  • Related