[英]Save Toggle Switch / Checkbox State with SharedPreferences
因此,我是一个初学者,这是我的第一个应用程序,无法保存拨动开关的状态。 当我退出应用程序时,它会清除切换状态,并使其看起来好像尚未切换。 我到处都在寻找解决方案,但是当我尝试实施建议的解决方案时,我会出错并崩溃。 与代码相比,我看到的所有示例看起来都很简单,所以我不知道该怎么做。
以前检查了这些线程:
如何在Android中保存switch(button)的状态
如果有人可以指出解决方案,我将非常高兴。 这是相关代码的片段:
@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://stackoverflow.com/a/23024962/6388980
如果您想了解有关使用sharedpreferences作为存储的更多信息,请参见此处:
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中检索此选中/未选中状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.