通过监听器更新共享preferences摘要

通过监听器更新共享preferences摘要

本文介绍了安卓:通过监听器更新共享preferences摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有更新的共享preferences为preference更改的摘要行的几个问题。我在onResume()的注册OnShare preferenceChangeListener,和同样的在的onPause(一个取消注册)。

I am having a few issues with updating the summary line in the SharedPreferences as a preference changes. I have a registered OnSharePreferenceChangeListener in the onResume(), and an unregister of the same in the onPause().

监听器是否正常工作,我能够使用onShared preferenceChanges()方法。我遇到的问题是能够检索preference有这样我就可以调用setSummary()。我在冰淇淋三明治,它看起来好像查找preference(key)方法是pcated德$ P $。所以:

The listener is functioning, and I am able to use the onSharedPreferenceChanges() method. The issue I am having is being able to retrieve the preference there so that I can call setSummary(). I am in Ice Cream Sandwich, and it appears as though the findPreference(key) method is deprecated. So:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);}

不正常,实际上返回null preF。从我所看到的例子,你需要得到一个preference调用setSummary()就可以了,想法?

is not functioning, and actually returns null for pref. From the examples I have seen, you need to get a preference to call setSummary() on it, and ideas?

推荐答案

您不应该使用 onShared preferenceChangedListener 这一点。

相反,使用类似的东西了这一点。

Instead, use something similar to this.

ListPreference myPreference = (ListPreference) findPreference("preference_key");
myPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (((String)newValue).equals("some_value")) {
            preference.setSummary("my summary");
        }
    }
});

找到preference 不是去precated,而是你不应该使用 preferenceActivity (即德precated)。如果你只需要支持Android 3.0以上版本,那么你应该切换到 preferenceFragment 的,新的方法。如果你需要支持Android 2.1+那么它是很好,你可以忽略这些警告。

findPreference is not deprecated, but rather you shouldn't be using a PreferenceActivity (that is deprecated). If you only need to support Android 3.0+ then you should switch to PreferenceFragment's, the new method. If you need to support Android 2.1+ then it is fine and you can ignore the warnings.

这篇关于安卓:通过监听器更新共享preferences摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:12