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

标签 java android android-studio sharedpreferences togglebutton

所以,我是一个初学者,这是我的第一个应用程序,我在保存切换开关的状态时遇到了麻烦。当我退出应用程序时,它会清除切换状态并使其看起来好像尚未切换。我已经仔细寻找解决方案,但是当我尝试实现其建议的方式时,我遇到了错误和崩溃。与我的代码相比,我看到的所有示例看起来都很简单,所以我不知道如何做到这一点。

之前查看过这些线程:

Save SWITCH button state, and recover state with SharedPrefs

Sharedpreferences with switch button

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

how to save the state of switch(button) in android

如果有人能给我指出一个解决方案,我会非常高兴。以下是相关代码 fragment :

    @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);
            }
        }
    });

最佳答案

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

https://stackoverflow.com/a/23024962/6388980

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

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

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

现在,在 setOnCheckedListener 中,每当调用 OnCheckedListener 时,您都希望在 Sharedpreferences 中写入内容。这样您就可以从 OnCreate() 方法中的 Sharedpreferences 检索此选中/未选中状态。

关于java - 使用 SharedPreferences 保存切换开关/复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628737/

相关文章:

java - Android编译,方法太多

java - 如何将 vue dist 文件夹部署到 GlassFish 5?

android - 如何在 Android Studio 设备模拟器中启动 'telnet' 控制台?

android - React Native for Android 中的依赖错误

java - Android 中根据用户输入更新 fragment

android - 使用 Android Studio 将 github 项目集成到我的应用程序中

java - 如何自动注册新的子类?

java.lang.ClassCastException : android. widget.ScrollView 无法转换为 android.widget.TextView

android - 如何将 Android DataBinding 绑定(bind)到菜单?

java - 使用 ARCore 检测平面和点击事件