dimanche 31 mai 2020

Deselecting checkboxes

I have 2 check boxes, I save them using sharedPreferences, however, I need one check box to be deselected if the other one is selected.

My code:

public static CheckBox checkHolyPaiges, checkDefault;
public static boolean checkHolyPaiges2, checkDefault2;

checkDefault = (CheckBox) findViewById(R.id.checkDefault);
    checkHolyPaiges = (CheckBox) findViewById(R.id.checkHolyPaiges);

final SharedPreferences prefs = getSharedPreferences("game", MODE_PRIVATE);

    if (checkHolyPaiges.isChecked())
    {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("checkHolyPaiges", true);
        editor.apply();

        checkDefault.setChecked(false);
    }

    if (checkDefault.isChecked())
    {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("checkDefault", true);
        editor.apply();

        checkHolyPaiges.setChecked(false);
    }

    checkHolyPaiges2 = prefs.getBoolean("checkHolyPaiges", false);
    checkDefault2 = prefs.getBoolean("checkDefault", true);



Aucun commentaire:

Enregistrer un commentaire