2016-09-22 105 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

相關問題