Android:添加和删除 SharedPreference

发布于 2024-11-06 14:36:15 字数 2005 浏览 6 评论 0原文

我在比较 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-11-13 14:36:15

要比较两个 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 Comparison

Good luck!

匿名。 2024-11-13 14:36:15

在 Java 中比较字符串时,请使用对象的 equals 方法,

例如if ( string1.equals(string2) ) { ... }

When comparing strings in Java use the equals method of Object

e.g. if ( string1.equals(string2) ) { ... }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文