我是初学者这是我的第一个应用程序我在保存拨动开关的状态时遇到了麻烦。当我退出应用程序时,它清除了切换状态,使它看起来像没有被切换。我一直在寻找解决方案,但当我试图实现它的建议时,我得到了错误和崩溃。与我的代码相比,我看到的所有示例看起来都很简单,所以我不知道如何做到这一点。
查看这些线程:
保存SWITCH按钮状态,并使用SharedPrefs恢复状态
Sharedpreferences with switch button
如何保存toollebutton on/off选择的状态?
如何保存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"。编辑器"pair"来保存错误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参照
在onCreate()方法中,如果交换机存在,您希望从Sharedpreferences中检索交换机的过去状态(假设您已经在那里编写了交换机的状态)。一旦你从首选项中获取状态,你必须在OnCreate中使用Switch的setChecked(boolean checked)方法来设置按钮的checked状态。这里的文档:https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)
现在在你的setOnCheckedListener中,你想在OnCheckedListener被调用时写入Sharedpreferences。这样你就可以在你的OnCreate()方法中从Sharedpreferences中检索这个选中/未选中的状态。