Android SharedPreferences with RadioButton

3

I have two RadioButtons, I want to select one of them, close the app and when I open load the last selection.

Can you help me? I can not find a clear tutorial explained.

Thank you.

    
asked by anonymous 11.05.2016 / 22:00

1 answer

0

The example I'm going to give is checkbox , but the method is no different, just change to its radiobutton .

Create a preference class. In it you can store everything you want. Here is my example:

public class Preferencia {
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences("energy", Context.MODE_PRIVATE);
    }

  //// preferencia de checkbox
   public static boolean setcbremember(Context context, boolean a) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("Cbremember", a);
        editor.commit();
        return a;

    }
    public static boolean getcbremember(Context context) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        return sharedPref.getBoolean("Cbremember", true);
    }
 //// Deletar preferencia de login e senha
        public static void deletar(Context context, String login) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.clear();
        editor.commit();

    }

}

Where I save checkbox , change to radiobutton and with an if and else, give shared what the user chooses.

Here is where I get the checkbox to assign to preference:

public void btnentrarclick(View currentButton) {

        if(cbremember.isChecked()) {
            AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                    edtlogin.getText().toString(), edtsenha.getText().toString());
            Preferencia.setlogin(Tela_de_Login.this, edtlogin.getText().toString());
            Preferencia.setsenha(Tela_de_Login.this, edtsenha.getText().toString());
            Preferencia.setcbremember(Tela_de_Login.this, cbremember.isChecked());

            ac.execute("");
        }
        else{

            AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                    edtlogin.getText().toString(), edtsenha.getText().toString());
            Preferencia.deletar(Tela_de_Login.this, edtlogin.getText().toString());
            Preferencia.deletar(Tela_de_Login.this, edtsenha.getText().toString());

            ac.execute("");
        }

    }

In my case, as you can see, if the user opts for checkbox triggered, it will save the user information, if he unchecks the checkbox, it will not save and erase what is saved. Hope it helped

    
12.05.2016 / 14:38