Android:通过对话框更改首选项不会更新值

时间:2013-12-01 21:47:10

标签: android sharedpreferences preferenceactivity

我在我的应用中使用了PreferenceActivity。定义settings.xml如下 -

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Preferences" >

<PreferenceCategory android:title="Email Options" >

    <EditTextPreference
        android:dialogTitle="@string/pref_email_user_title"
        android:key="pref_email_user"
        android:summary="@string/pref_email_user_summary"
        android:persistent="true" 
        android:title="@string/pref_email_user_title" />

    <EditTextPreference
        android:dialogTitle="@string/pref_email_password_title"
        android:key="pref_email_password"
        android:summary="@string/pref_email_password_summary"
        android:persistent="true"
        android:title="@string/pref_email_password_title" />
</PreferenceCategory>

活动如下 -

public class Prefs extends PreferenceActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    }
}

你需要的是什么,对吧?显然不是,因为我点击“电子邮件用户”设置会发生一个对话框,我用它来输入一个字符串值。但是我输入的内容没有放在“设置”屏幕上,我仍然看到默认值。

我注意到这些值会在/data/data/<package>/shared_prefs

下的首选项文件中保留

当我重新启动应用程序时,我也没有看到这些持久值 - 它显示默认值。我缺少什么魔法?

1 个答案:

答案 0 :(得分:0)

您需要OnPreferenceChangeListener来监听偏好设置的更改并自动更新偏好设置摘要。

请参阅SDK中的示例中的以下代码:

/**
 * A preference value change listener that updates the preference's summary
 * to reflect its new value.
 */
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference.setSummary(
                    index >= 0
                            ? listPreference.getEntries()[index]
                            : null);

        } else if (preference instanceof RingtonePreference) {
            // For ringtone preferences, look up the correct display value
            // using RingtoneManager.
            if (TextUtils.isEmpty(stringValue)) {
                // Empty values correspond to 'silent' (no ringtone).
                preference.setSummary(R.string.pref_ringtone_silent);

            } else {
                Ringtone ringtone = RingtoneManager.getRingtone(
                        preference.getContext(), Uri.parse(stringValue));

                if (ringtone == null) {
                    // Clear the summary if there was a lookup error.
                    preference.setSummary(null);
                } else {
                    // Set the summary to reflect the new ringtone display
                    // name.
                    String name = ringtone.getTitle(preference.getContext());
                    preference.setSummary(name);
                }
            }

        } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }
};

您需要将此绑定到所有首选项,例如:

bindPreferenceSummaryToValue(findPreference("username"));

/**
 * Binds a preference's summary to its value. More specifically, when the
 * preference's value is changed, its summary (line of text below the
 * preference title) is updated to reflect the value. The summary is also
 * immediately updated upon calling this method. The exact display format is
 * dependent on the type of preference.
 * 
 * @see #sBindPreferenceSummaryToValueListener
 */
private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getString(preference.getKey(), ""));
}