使用 SharedPreferences 保存切换开关/复选框状态

Save Toggle Switch / Checkbox State with SharedPreferences

所以,我是初学者,这是我的第一个应用程序,我在保存拨动开关的状态时遇到了问题。当我退出应用程序时,它会清除切换状态并使其看起来好像没有被切换。我已经四处寻找解决方案,但是当我尝试实施其建议的方式时,我遇到了错误和崩溃。与我的代码相比,我看到的所有示例看起来都很简单,所以我不知道该怎么做。

之前检查过这些线程:

Sharedpreferences with switch button

How to save the state of the toogleButton on/off selection?

如果有人能指出我的解决方案,我将非常高兴。这是相关代码的片段:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        switchButton_Stat = (Switch) findViewById(switch_s);
        textView_S = (TextView) findViewById(R.id.textView_S);
        textView_S.setText(switchOff);

        switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
                if (bChecked) {
                    textView_S.setText(switchOn);
                    CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d);
                    if (toggle_d.isChecked()){
                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_mon.removeCallbacks(runnable_mon);
                        handler_sun.postDelayed(runnable_sun,300);
                    }
                    else {
                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_sun.removeCallbacks(runnable_sun);
                        handler_mon.postDelayed(runnable_mon,300);
                    }
                }
                else {
                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_mon.removeCallbacks(runnable_mon);
                    handler_sun.removeCallbacks(runnable_sun);
                    textView_S.setText(switchOff);
                }
            }
        });


        switchButton_Day = (Switch) findViewById(R.id.switch_d);
        textView_D = (TextView) findViewById(R.id.textView_D);
        textView_D.setText(MonOff);

        switchButton_Day.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean dChecked) {
                if (dChecked) {
                    textView_D.setText(SunOff);
                    Switch switch_s = (Switch) findViewById(R.id.switch_s);
                    if (switch_s.isChecked()){

                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_mon.removeCallbacks(runnable_mon);
                        handler_sun.postDelayed(runnable_sun,300);
                    }

                    Log.i(TAG, "Day Switch");
                } else {
                    textView_D.setText(MonOff);
                    Switch switch_s = (Switch) findViewById(R.id.switch_s);
                    if (switch_s.isChecked()){

                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        //NM.cancel(2);
                        handler_sun.removeCallbacks(runnable_sun);
                        handler_mon.postDelayed(runnable_mon,300);
                    }
                }
            }
        });

    }

好的,所以我想我知道我做错了什么......在我之前的尝试中,我添加了 "SharedPreferences.Editor" 对以在错误的 if/else 语句中保存切换状态因为我有 if/else 语句相互嵌套。下面是实现

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switchButton_Stat = (Switch) findViewById(switch_s);

    SharedPreferences tog_prefs = getSharedPreferences("TOG_PREF", MODE_PRIVATE);
    boolean tglpref_1 = tog_prefs.getBoolean("tglpref_1", false);
    if (tglpref_1) {
        switchButton_Stat.setChecked(true);
    } else {
        switchButton_Stat.setChecked(false);
    }


    switchButton_Stat = (Switch) findViewById(switch_s);
    textView_S = (TextView) findViewById(R.id.textView_S);
    textView_S.setText(switchOff);

    switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
            if (bChecked) {
                SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit();
                editor.putBoolean("tglpref_1", true); // value to store
                editor.commit();

                textView_S.setText(switchOn);
                CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d);
                if (toggle_d.isChecked()){


                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_mon.removeCallbacks(runnable_mon);
                    handler_sun.postDelayed(runnable_sun,300);
                }
                else {
                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_sun.removeCallbacks(runnable_sun);
                    handler_mon.postDelayed(runnable_mon,300);
                }
            }
            else {
                SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit();
                editor.putBoolean("tglpref_1", false); // value to store
                editor.commit();

                NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NM.cancelAll();
                handler_mon.removeCallbacks(runnable_mon);
                handler_sun.removeCallbacks(runnable_sun);
                textView_S.setText(switchOff);
            }
        }
    });

Sharedpreferences 是一种存储选项。要在 sharedpreferences 中写入和检索信息,此答案将帮助您:

如果您想了解有关使用共享首选项作为存储的更多信息,请查看此处:

https://developer.android.com/guide/topics/data/data-storage.html#pref

在 onCreate() 方法中,您希望从 Sharedpreferences 中检索 Switch 过去的状态(如果它存在于那里)(假设您已经在那里写入了开关的状态)。从首选项中检索状态后,您必须在 OnCreate 中使用 Switch 的 setChecked(boolean checked) 方法来设置按钮的选中状态。此处的文档:https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

现在在您的 setOnCheckedListener 中,您希望在调用 OnCheckedListener 时写入 Sharedpreferences。通过这种方式,您可以在 OnCreate() 方法中从 Sharedpreferences 检索此 checked/unchecked 状态。