本文介绍了刷新安卓preferenceFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框preference 片段preference 。当用户激活该复选框,我检查是否已安装应用程序,如果没有,我重置假preference和我开的 Play商店的下载应用程序。

I have a CheckBoxPreference in a FragmentPreference. When the user active the check box, I check if an application is installed and if not, I reset the preference on false and I open the Play Store to download the app.

基本上所有工作正常,但我有一个问题,刷新UI。事实上,即使我设置了preference假前打开的 Play商店的,当用户回来,选中此复选框(片段刚刚被暂停和恢复所以preference值被忽略)。

Basically all works fine, but I have an issue to refresh the UI. Indeed, even if I set the preference on false before opened the Play Store, when the user come back, the box is checked (the fragment has just been paused and resumed so the preference value is ignored).

有没有一种方法,以刷新的活动或片段?

Is there a way to "refresh" the activity or the fragment?

推荐答案

请您preferenceFragment实施<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.OnShared$p$pferenceChangeListener.html">OnShared$p$pferenceChangeListener接口。

Make your PreferenceFragment implement the OnSharedPreferenceChangeListener interface.

的onCreate(),你可以阅读preference值,并设置相应的用户界面。例如:

In onCreate(), you can read the preference value and set the UI accordingly. For instance:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource.
    addPreferencesFromResource(R.xml.shakesql_app_prefs);

    // Initialize pref summary label.

    // Get a reference to the application default shared preferences.
    SharedPreferences sp = getPreferenceScreen().getSharedPreferences();

    // Read the current preference value.
    String listVal =
            sp.getString(getString(R.string.preference_font_size_key),
                    getString(R.string.preference_font_size_default));

    // Look up the textual description corresponding to the
    // preference value and write it into the summary field.
    String listDesc = descriptionFromPref(
            listVal, 
            getResources().getStringArray(
                    R.array.preference_font_size_values), 
                    getResources().getStringArray(
                            R.array.preference_font_size_labels)
                    );
    if (!TextUtils.isEmpty(listDesc)) {
        ListPreference listPref =
                (ListPreference) findPreference(getString(R.string.preference_font_size_key));
        listPref.setSummary(listDesc);
    }
}

然后,在 onShared preferenceChanged(),更新UI。

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Preference pref = findPreference(key);
    if (pref instanceof ListPreference) {
        // Update display title
        // Write the description for the newly selected preference 
        // in the summary field.
        ListPreference listPref = (ListPreference) pref;
        CharSequence listDesc = listPref.getEntry();
        if (!TextUtils.isEmpty(listDesc)) {
            pref.setSummary(listDesc);
        }
    }        
}

下面是从API演示高级preferences样品code段给力的复选框preference的价值。

Here's a code snippet from the AdvancedPreferences sample in API Demos to force the value of a checkbox preference.

// Toggle the value of mCheckBoxPreference.
if (mCheckBoxPreference != null) {
            mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
}

这篇关于刷新安卓preferenceFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:03