Android listpreferences,如何保存个人偏好

发布于 2024-12-20 07:19:44 字数 886 浏览 0 评论 0原文

我有一些列表首选项,但我不知道如何保存列表中的各个值。我该怎么做?这是我的

http://i41.tinypic.com/dh4gvo.png

    Preference customPref = (Preference) findPreference("notificationPref");
    customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {

            SharedPreferences customSharedPreference = getSharedPreferences(
                    "notifications", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = customSharedPreference
                    .edit();
            editor.putString("notification",
                    "The preference has been clicked");
            editor.commit();
            return true;
        }

    });

我的列表单击侦听器仅适用于列表首选项页面中的主项目,而不适用于弹出窗口本身中的项目。如何保存在弹出窗口中选择的选项?

I have some list preferences, but I don't know how to save the individual values from the list. How do I do it? Here is what I have

http://i41.tinypic.com/dh4gvo.png

    Preference customPref = (Preference) findPreference("notificationPref");
    customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {

            SharedPreferences customSharedPreference = getSharedPreferences(
                    "notifications", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = customSharedPreference
                    .edit();
            editor.putString("notification",
                    "The preference has been clicked");
            editor.commit();
            return true;
        }

    });

my list click listener is only for the main item in the list preferences page, but not the items in the popup itself. How do I save the choice selected in the popup itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

轮廓§ 2024-12-27 07:19:44

这通常是自动的。在您的首选项屏幕 XML 中,您应该有如下内容:

<ListPreference android:title="@string/Title" 
    android:summary="@string/Summary"
    android:key="PreferenceKey"
    android:defaultValue="VALUE_2"
    android:entries="@array/Entries"
    android:entryValues="@array/Values" />

在您的 strings.xml 中:

<string name="Value1">Text for value 1</string>
<string name="Value2">Text for value 2</string>
<string name="Value3">Text for value 3</string>

<string-array name="Entries">
            <item>@string/Value1</item>
            <item>@string/Value2</item>
            <item>@string/Value2</item>
</string-array>
<string-array name="Values">
            <item>VALUE_1</item>
            <item>VALUE_2</item>
            <item>VALUE_3</item>
</string-array>

“Values”数组指定保存在首选项中的(字符串)值,而“Entries”数组指定显示到屏幕的项目的文本。用户。每次用户选择一个项目时,“Values”数组中的相应值都会保存到指定键(本例中为“PreferenceKey”)下的首选项中。

This is usually automatic. In your preference screen XML, you should have something like this:

<ListPreference android:title="@string/Title" 
    android:summary="@string/Summary"
    android:key="PreferenceKey"
    android:defaultValue="VALUE_2"
    android:entries="@array/Entries"
    android:entryValues="@array/Values" />

And in your strings.xml:

<string name="Value1">Text for value 1</string>
<string name="Value2">Text for value 2</string>
<string name="Value3">Text for value 3</string>

<string-array name="Entries">
            <item>@string/Value1</item>
            <item>@string/Value2</item>
            <item>@string/Value2</item>
</string-array>
<string-array name="Values">
            <item>VALUE_1</item>
            <item>VALUE_2</item>
            <item>VALUE_3</item>
</string-array>

The "Values" array specify the (string) value saved in preferences, whereas the "Entries" array specify the text of the items displayed to the user. Each time the user select an item, its corresponding value in the "Values" array is saved to preferences under the specified key ("PreferenceKey" in this example).

最丧也最甜 2024-12-27 07:19:44

您可以这样阅读偏好设置...

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

String strSavedMem1 = sharedPreferences.getString("key", "Default Value");

You can read preferences like this...

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

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