Android:添加和删除 SharedPreference
我在比较 OnSharedPreferenceChanged 方法中的关键字符串时遇到了一些问题,希望您能够看一下它 - 基本上问题是,即使我知道并且可以控制台记录用于比较的确切字符串,语句块中的代码永远不会触发 - 我知道这是愚蠢的事情,但我只是看不到它,
提前感谢
奥比,
这是代码
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;
}
}
I'm having a bit of an issue comparing key strings in an OnSharedPreferenceChanged method and hoped you might be able to take a look at it - basically the problem is that even when i know and can console log the exact string for the comparison, the code in the statement block never fires - i know it's something stupid but i just can't see it
thanks in advance
obie
here's the code
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要比较两个 String 对象,您必须使用 string1.equals(string2)。以下链接可能对您有用: Java 字符串比较
祝你好运!
To compare two String objects you must use
string1.equals(string2)
. Here's a link you might find useful: Java String ComparisonGood luck!
在 Java 中比较字符串时,请使用对象的
equals
方法,例如
if ( string1.equals(string2) ) { ... }
When comparing strings in Java use the
equals
method of Objecte.g.
if ( string1.equals(string2) ) { ... }