How to save (switch) button state in android?

后端 未结 2 660
孤街浪徒
孤街浪徒 2021-01-05 06:13

I am using SWITCH (like android toggle button ) instead of normal buttons in my andorid app. The code works fine while enabling and disabling switches. But i want to store t

2条回答
  •  生来不讨喜
    2021-01-05 06:55

              SharedPreferences pref = getSharedPreferences("save",MODE_PRIVATE);
                        unit.setChecked(pref.getBoolean("first", false));
    
                                if(isChecked) {
                     SharedPreferences.Editor editor = getSharedPreferences("save"MODE_PRIVATE).edit();
                                        editor.putBoolean("first", true);
                                        editor.apply();
                                        unit.setChecked(true);}
    
    else
    {
        SharedPreferences.Editor editor = getSharedPreferences("save",MODE_PRIVATE).edit();
                                    editor.putBoolean("first",false);
                                    editor.apply();
                                    kilometer.setText("Km/h");
                                    unit.setChecked(false);`enter code here`
    }
    

提交回复
热议问题