所以,我是初學者,這是我的第一個應用程序,我無法保存切換開關的狀態。當我退出應用程序時,它將清除切換狀態,並使其看起來好像未被切換。我已經找遍瞭解決方案,但是當我嘗試實現它的建議時,我會遇到錯誤和崩潰。我所看到的所有例子與我的代碼相比看起來很簡單,所以我不知道如何去做。用SharedPreferences保存切換開關/複選框狀態
經過這些線程之前:
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);
}
}
});
嘿有多少'Switch'(s)在那裏?一個或多個? – tpk
有2個開關 – UberNoob
所有最好的和快樂的編碼。 – tpk