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

안녕하세요, 얼마나 많은 '스위치'가 있습니까? 하나 이상의 하나 ?? – tpk

+0

스위치가 두 개 있습니다. – UberNoob

+0

모두 최상 및 해피 코딩입니다. – tpk

답변

1

Sharedpreferences는 하나의 저장 옵션입니다.

https://stackoverflow.com/a/23024962/6388980

을 그리고 당신이 된 SharedPreferences 사용에 대한 자세한 정보를 원하는 경우 스토리지로 이쪽을 봐 : 작성 된 SharedPreferences에서 정보를 검색하려면이 답변이 도움이 될 것입니다

하지 OnCreate https://developer.android.com/guide/topics/data/data-storage.html#pref

() 메서드를 사용하여 스위치의 과거 상태를 Sharedpreferences에서 검색 할 수 있습니다 (스위치의 상태를 작성했다고 가정). 환경 설정에서 상태를 검색 한 후에는 OnCreate 내부에서 스위치의 setChecked (boolean checked) 메소드를 사용하여 버튼의 선택 상태를 설정해야합니다. 여기에 문서 : https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

이제 setOnCheckedListener에서 OnCheckedListener가 호출 될 때마다 SharedPreferences에 기록하려고합니다. 이 방법으로 OnCreate() 메서드의 SharedPreferences에서이 체크/체크되지 않은 상태를 검색 할 수 있습니다.

+0

도움을 주셔서 감사합니다. – UberNoob