2013-07-24 90 views
0

編輯:我試過這個新的代碼,但它仍然似乎他們被重置。任何人都可以告訴我,並提出解決方案,我的設置被重置?偏好活動設置不保存

public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener{ 

    static SwitchPreference mucus_stamps; 
    static SwitchPreference fertile_infertil; 
    static SwitchPreference cervical_mucus; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    /** 
    * Populate the activity with the top-level headers. 
    */ 
    @Override 
    public void onBuildHeaders(List<Header> target) { 
     Log.i("PROJECTCARUSO","onBuildHeaders"); 
     loadHeadersFromResource(R.xml.preference_headers, target); 

    } 

    /** 
    * This fragment shows the preferences for the first header. 
    */ 
    public static class Prefs1Fragment extends PreferenceFragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Load the preferences from an XML resource 
      addPreferencesFromResource(R.xml.notification_settings); 
     } 
    } 

    /** 
    * This fragment shows the preferences for the second header. 
    */ 
    public static class Prefs2Fragment extends PreferenceFragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Load the preferences from an XML resource 
      addPreferencesFromResource(R.xml.charting_settings); 

      //findPreference("cervical_mucus").setOnPreferenceChangeListener(
        mucus_stamps = (SwitchPreference) findPreference("mucus_stamps"); 
        fertile_infertil = (SwitchPreference) findPreference("fertile_infertil"); 
        cervical_mucus = (SwitchPreference) findPreference("cervical_mucus"); 

        cervical_mucus.setOnPreferenceChangeListener(
           new OnPreferenceChangeListener() { 

            @Override 
            public boolean onPreferenceChange(Preference preference, Object newValue) { 

             // string test w/o "==" usage. 
             if (newValue.equals(false)) { 
              Log.i("PROJECTCARUSO","false"); 
              mucus_stamps.setChecked(false); 
              fertile_infertil.setChecked(false); 

             } else { 
              Log.i("PROJECTCARUSO","true"); 
              mucus_stamps.setChecked(true); 
              fertile_infertil.setChecked(true); 
             } 

             // true instead of false so the new value gets kept 
             return true; 
            }; 
           }); 


         if(!cervical_mucus.isChecked()){ 
          mucus_stamps.setSelectable(false); 
          mucus_stamps.setEnabled(false); 

          fertile_infertil.setSelectable(false); 
          fertile_infertil.setEnabled(false); 
         } else { 
          mucus_stamps.setSelectable(true); 
          mucus_stamps.setEnabled(true); 

          fertile_infertil.setSelectable(true); 
          fertile_infertil.setEnabled(true); 
         } 
     } 
    } 

    protected void onPause() 
    { 
     super.onPause(); 
    } 

    protected void onResume() 
    { 
     super.onResume(); 
    } 


    @Override 
    public boolean onPreferenceChange(Preference arg0, Object arg1) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <PreferenceCategory 
     android:title="@string/pref_chart_profile" 
     android:textSize="20px"> 

     <SwitchPreference 
       android:title="@+string/pref_symptothermal" 
       android:summary="@+string/pref_symptothermal_summary" 
       android:key="symptothermal" 
       android:defaultValue="true" 
       android:layout="@layout/pref_layout"/> 

     <SwitchPreference 
       android:id="@+id/cervical_mucus" 
       android:title="@+string/pref_cervical_mucus" 
       android:summary="@+string/pref_cervical_mucus_summary" 
       android:key="cervical_mucus" 
       android:defaultValue="true" 
       android:layout="@layout/pref_layout" />  

     <SwitchPreference 
       android:id="@+id/mucus_stamps" 
       android:title="@+string/pref_mucus_stamps" 
       android:summary="@+string/pref_mucus_stamps_summary" 
       android:key="mucus_stamps" 
       android:defaultValue="true" 
       android:layout="@layout/pref_layout" /> 

     <SwitchPreference 
       android:id="@+id/fertile_infertil" 
       android:title="@+string/pref_fertile_infertile" 
       android:summary="@+string/pref_fertile_infertile_summary" 
       android:key="fertile_infertil" 
       android:defaultValue="true" 
       android:layout="@layout/pref_layout" /> 
    </PreferenceCategory> 

</PreferenceScreen> 

回答

1

取決於你想要什麼。

該值被保存(測試),但「可選」不是優先值。因此,您必須在首選項屏幕啓動時進行設置。

這樣做: 您的OnCreate之前:

SwitchPreference mucus_stamps; 

那麼你findPreference以上( 「mucus_stamps」)現在

mucu_stamps = (SwitchPreference) findPreference("mucus_stamps"); 

,該onclicklistener外,你可以設置它。

if(!mucus_stamps.isChecked()){ 
      mucus_stamps.setSelectable(false); 
      mucus_stamps.setEnabled(false); 
     } 

(我補充說:「setEnablet」,以示你,你可以藏漢禁用它,不只是使其「不可選」。可選擇只說你不能點擊任何東西外面「OFF/ON」區域將其更改爲開/關)

個人而言,我更願意做這樣的:

mucus_stamps = (SwitchPreference) findPreference("mucus_stamps"); 
    mucus_stamps.setOnPreferenceChangeListener(
       new OnPreferenceChangeListener() { 

        @Override 
        public boolean onPreferenceChange(Preference preference, Object newValue) { 

         //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 




         // string test w/o "==" usage. 
         if (newValue.equals(false)) { 
          mucus_stamps.setChecked(false); 

          mucus_stamps.setSelectable(false); 
         } else { 
          mucus_stamps.setChecked(true); 
         } 

         // true instead of false so the new value gets kept 
         return true; 
        }; 
       }); 


     if(!mucus_stamps.isChecked()){ 
      mucus_stamps.setSelectable(false); 
      mucus_stamps.setEnabled(false); 
     } 

而且,你並不需要實現OnPreferenceChangeListener當你做到這一點,你還是我做的。並不是說它真的很重要。

:)

+0

進行了編輯,但仍然重新設置,當我回到它。看到我上面的新代碼 – jcaruso