gpt4 book ai didi

Android listpreferences,如何保存个人偏好

转载 作者:行者123 更新时间:2023-11-29 14:55:12 25 4
gpt4 key购买 nike

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

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;
}

});

我的列表点击监听器仅针对列表首选项页面中的主要项目,而不是弹出窗口中的项目本身。如何保存在弹出窗口中选择的选项?

最佳答案

这通常是自动的。在您的首选项屏幕 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”数组中的相应值将保存到指定键(本例中为“PreferenceKey”)下的首选项中。

关于Android listpreferences,如何保存个人偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8419117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com