所以,我是初学者,这是我的第一个应用程序,我在保存切换开关状态时遇到问题。当我退出应用程序时,它会清除切换的状态并使其看起来好像没有被切换。我已经全神贯注地寻找解决方案但是当我尝试实现它的建议我得到错误和崩溃时。我看到的所有示例与我的代码相比都很简单,所以我无法弄清楚如何做到这一点。
之前检查过这些主题:
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
如果有人能指出我的解决方案,我会非常高兴。以下是相关代码的片段:
@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);
}
}
});
答案 0 :(得分:1)
Sharedpreferences是一个存储选项。要在共享偏好中编写和检索信息,这个答案将对您有所帮助:
https://stackoverflow.com/a/23024962/6388980
如果您想了解有关使用sharedpreferences作为存储的更多信息,请点击此处:
https://developer.android.com/guide/topics/data/data-storage.html#pref
在onCreate()方法中,您希望从Sharedpreferences中检索Switch的过去状态(如果它存在)(假设您已在其中写入了切换的状态)。从首选项中检索状态后,必须使用OnCreate中的Switch的setChecked(布尔检查)方法来设置按钮的已检查状态。这里的文档:https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)
现在,在setOnCheckedListener中,只要调用OnCheckedListener,就要在Sharedpreferences中写入。这样,您可以在OnCreate()方法中从Sharedpreferences中检索此已选中/未选中状态。