我在比較OnSharedPreferenceChanged方法中的關鍵字符串時遇到了一些問題,並希望您能夠看一看它 - 基本上問題是即使當我知道並且可以控制檯日誌的比較確切的字符串,在聲明塊中的代碼永遠不會觸發 - 我知道這是愚蠢的東西,但我看不出會事先Android:添加和刪除SharedPreference
感謝
奧比
這裏的代碼
public class WallpaperSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{
public static final String TAG = "WallpaperSettings";
private static final String USE_CLOCK_TEXT = "useClockText";
private Preference _clockTextPosPicker;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(HexClockWallpaper.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.hexclockwallpaper_settings);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onDestroy()
{
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key)
{
// [Log] key: 'useClockText'
Log.i(TAG, "key: '" + key + "'");
if (key == USE_CLOCK_TEXT)
{
// None of this is ever seen
Log.i(TAG, "key == " + USE_CLOCK_TEXT);
Boolean selected = sharedPreferences.getBoolean(key, true);
Log.i(TAG, "selected changed: " + selected);
if (selected)
{
getPreferenceScreen().removePreference(getClockTextPicker());
}
else
{
getPreferenceScreen().addPreference(getClockTextPicker());
}
}
}
public Preference getClockTextPicker()
{
if (_clockTextPosPicker == null)
_clockTextPosPicker = findPreference("clockTextPosPicker");
return _clockTextPosPicker;
}
}
這是什麼問題?你有什麼試圖解決它? – 2011-05-12 16:09:27