2

我正在使用 andengine 制作动态壁纸。问题出在设置屏幕上。当我在预览屏幕中按下设置按钮时,我的 onSharedPreferenceChanged() 方法会自动调用..我认为除非我更改某些首选项,否则不应该发生这种情况。下面是我的实现。有人可以告诉我我在做什么错吗?

public class LiveWallpaperSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener{

ListPreference listPreferencespeed, listPreferenceConst, listPreferenceCld;
public static boolean mSettingsChanged = false;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);        
    listPreferencespeed = (ListPreference) findPreference("listPref");
    listPreferenceConst= (ListPreference) findPreference("listPrefConst");
    listPreferenceCld = (ListPreference) findPreference("listPrefCld");

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    Log.e("LiveWallpaperSettings", "Key :- " + key);
    SharedPreferences customSharedPreference = getSharedPreferences(key, LiveWallpaperSettings.MODE_PRIVATE);
        SharedPreferences.Editor editor = customSharedPreference.edit();
        editor.putString("Speed",listPreferencespeed.getValue());
        editor.putString("Cld", listPreferenceCld.getValue());
        editor.putString("Const", listPreferenceConst.getValue());
        editor.commit();
        mSettingsChanged = true;

    Log.e("LiveWallpaperSettings", "Speed Value after setting " + customSharedPreference.getString("Speed", ""));
    Log.e("LiveWallpaperSettings", "Cloud Value after setting " + customSharedPreference.getString("Cld", ""));
    Log.e("LiveWallpaperSettings", "Constellations Value after setting " + customSharedPreference.getString("Constd", ""));
    Log.e("LiveWallpaperSettings", "mSettingsChanged " + mSettingsChanged);
}



@SuppressWarnings("deprecation")
@Override
protected void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@SuppressWarnings("deprecation")
@Override
protected void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}



}
4

1 回答 1

0

问题可能不在您发布的代码中,而在于设置侦听器的代码中。在您的代码中查找类似的内容:
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
并检查是否正确放置。例如,如果您设置侦听器,然后初始化值,您可能希望颠倒该顺序。

于 2013-06-10T19:47:59.457 回答