2016-09-22 76 views
0

所以,我是初学者,这是我的第一个应用程序,我无法保存切换开关的状态。当我退出应用程序时,它将清除切换状态,并使其看起来好像未被切换。我已经找遍了解决方案,但是当我尝试实现它的建议时,我会遇到错误和崩溃。我所看到的所有例子与我的代码相比看起来很简单,所以我不知道如何去做。用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); 
      } 
     } 
    }); 
+0

嘿有多少'Switch'(s)在那里?一个或多个? – tpk

+0

有2个开关 – UberNoob

+0

所有最好的和快乐的编码。 – tpk

回答

1

Sharedpreferences是一个存储选项。要写入和检索在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中检索此选中/未选中状态。

+0

我明白了,谢谢你的帮助。 – UberNoob