3 回答
TA贡献1893条经验 获得超10个赞
prefs.registerOnSharedPreferenceChangeListener( new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // Implementation }});
// Use instance field for listener // It will not be gc'd as long as this instance is kept referencedlistener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // Implementation }};prefs.registerOnSharedPreferenceChangeListener(listener);
更新
TA贡献1799条经验 获得超8个赞
prefs = PreferenceManager.getDefaultSharedPreferences(this);
public class Preferences extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load the XML preferences file addPreferencesFromResource(R.xml.preferences); }}
@Overridepublic boolean onPrepareOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //start Preference activity to show preferences on screen startActivity(new Intent(this, Preferences.class)); //hook into sharedPreferences. THIS NEEDS TO BE DONE AFTER CREATING THE ACTIVITY!!! prefs.registerOnSharedPreferenceChangeListener(this); return false;}
注
TA贡献1802条经验 获得超5个赞
OnSharedPreferenceChangeListener myPrefListner = new OnSharedPreferenceChangeListener(){ public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // your stuff }};
@Override protected void onResume() { super.onResume(); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(myPrefListner); } @Override protected void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(myPrefListner);}
- 3 回答
- 0 关注
- 537 浏览
添加回答
举报