【问题标题】:Android: Changing a preference via dialog does not update the valueAndroid:通过对话框更改首选项不会更新值
【发布时间】:2013-12-01 21:47:10
【问题描述】:

我在我的应用中使用了 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/&lt;package&gt;/shared_prefs 下的首选项文件中

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

【问题讨论】:

    标签: android sharedpreferences preferenceactivity


    【解决方案1】:

    您需要一个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(), ""));
    }
    

    【讨论】:

    • 谢谢迈克。在应用程序运行时动态更改首选项是有意义的,但为什么重新启动应用程序时我的首选项没有加载?
    • 您的意思是,当您重新启动应用程序并进入设置时,您为什么看不到每个首选项的摘要?那是因为在bindPreferenceSummary 中有一些代码会触发一次侦听器。这将设置摘要(参见该方法的最后一行)。
    • 太好了,很享受。我看到其中一些方法已被弃用,例如查找首选项()。现在推荐的方法是使用 PreferenceFragments。但暂时我会使用上面的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多