2014-05-04 75 views
2

我需要改變檢查偏好活動箱內偏好,但它不似乎工作:更改CheckBoxPreference偏好活動中

public class SettingsActivity extends PreferenceActivity { 

    static CheckBoxPreference autoP; 


    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{ 



     @Override 
     public void onCreate(final Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.prefs); 


      autoP = (CheckBoxPreference)findPreference("autoP"); 
      autoP.setOnPreferenceChangeListener(autoP_listener); 

     OnPreferenceChangeListener autoP_listener = new OnPreferenceChangeListener() { 
      @Override 
      public boolean onPreferenceChange(Preference preference,Object newValue) { 

       autoP.setDefaultValue((String) newValue); 

       if (newValue.toString().equals("true")) 

         getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

        else 
         getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 




       return false; 
      } 
     }; 

    } 

} 

回答

2

這應該工作:

final CheckBoxPreference autoP = (CheckBoxPreference) getPreferenceManager().findPreference("autoP"); 

autoP.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {    
    public boolean onPreferenceChange(Preference preference, Object newValue) { 
     if ((Boolean)newValue == true) { 
      getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     }else { 
      getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     } 
     return true; 
    } 
}); 
0

試試這個:

public class SettingsActivity extends PreferenceActivity { 

    static CheckBoxPreference autoP; 


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

    public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { 

     @Override 
     public void onCreate(final Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.prefs); 

      autoP = (CheckBoxPreference)findPreference("autoP"); 
      autoP.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
       @Override 
       public boolean onPreferenceChange(Preference preference,Object newValue) { 
        updateOnPreferenceChange(); 
       } 
      }); 

     } 

     private void updateOnPreferenceChange() { 

      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
      String pref_name = "should_be_pref_name_in_xml_file"; 
      boolean checked = sharedPreferences.getBoolean(pref_name, defaultValue); 
      if (checked) { 
       // todo 
      } else { 
       // todo 
      } 
     } 
    } 
} 
0

試試這個

public class SettingsActivity extends PreferenceFragment { 


@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.prefs); 

} 

@Override 
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
     Preference preference) { 


    String key = preference.getKey(); 

    if (key.equals("key")) { 
     if (((CheckBoxPreference) preference).isChecked()){ 
      // do things if the checkbox is checked  
      getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } else { 
     // do things if it is unchecked 
     getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 
return true; 
    } 
}