Android获取sharedPreferences来存储用户选择
我想将选择的水果显示为可绘制的 我有它,这样可以保存,但我不知道如何从活动首选项中获取它 并使用它
我想可能会有类似fruit = getpreference的东西(类似这样的东西)
int resourceID = getResource.getIdentifier(fruit,"drawable",getPackageName());
favortieFruit.setImageResource(resourceID);
Options.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
public class Options extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.options);
//Get the custom preference
Preference customPref = (Preference) findPreference("listPref");
customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
SharedPreferences customSharedPreference = getSharedPreferences("Fruits", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.commit();
return true;
}
});
}
}
Options.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Fruit Selection" android:key="listPref">
<ListPreference android:title="Fruit #1" android:key="listPref1" android:defaultValue="digiGreen" android:entries="@array/Fruits" android:entryValues="@array/listValues" android:summary="Select Fruit #1 "/><ListPreference
android:title="Fruit #2"
android:summary="Select Fruit #2 "
android:key="listPref2"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" /><ListPreference
android:title="Fruit #3"
android:key="listPref3"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #3 "/><ListPreference
android:title="Fruit #4"
android:key="listPref4"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #4 "/><ListPreference
android:title="Fruit #5"
android:key="listPref5"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #5 "/><ListPreference
android:title="Fruit #6"
android:key="listPref6"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #6 "/>
</PreferenceCategory>
</PreferenceScreen>
strings.xml
<string-array name="Fruits">
<item>Apple</item>
<item>Pear</item>
<item>Grape</item>
<item >Berry</item>
<item >Tomato</item>
</string-array>
<string-array name="listValues">
<item>apple</item>
<item>pear</item>
<item>grape</item>
<item>berry</item>
<item>tomato</item>
</string-array>
====================== ======== 找到解决方案
Options.java
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Options extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
获取设置
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
for (int num = 1; num < 7; num++) {
String icon = sp.getString("listPref" + num, null);
try {
Class<drawable> res = R.drawable.class;
Field field = res.getField(icon);
PoliticiansArray[num] = field.getInt(null);
Class<string> stringRes = R.string.class;
Field stringField = stringRes.getField(icon);
SayingsArray[num] = stringField.getInt(null);
} catch (Exception e) {
Log.e("MyTag", "Failed", e);
}
I want to display the the fruit of choice as a drawable
i have it so that it saves but I don't know how to get it from the activity preference
and use it
I was thinking there might be somthing like fruit = getpreference (something like this)
int resourceID = getResource.getIdentifier(fruit,"drawable",getPackageName());
favortieFruit.setImageResource(resourceID);
Options.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
public class Options extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.options);
//Get the custom preference
Preference customPref = (Preference) findPreference("listPref");
customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
SharedPreferences customSharedPreference = getSharedPreferences("Fruits", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.commit();
return true;
}
});
}
}
Options.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Fruit Selection" android:key="listPref">
<ListPreference android:title="Fruit #1" android:key="listPref1" android:defaultValue="digiGreen" android:entries="@array/Fruits" android:entryValues="@array/listValues" android:summary="Select Fruit #1 "/><ListPreference
android:title="Fruit #2"
android:summary="Select Fruit #2 "
android:key="listPref2"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" /><ListPreference
android:title="Fruit #3"
android:key="listPref3"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #3 "/><ListPreference
android:title="Fruit #4"
android:key="listPref4"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #4 "/><ListPreference
android:title="Fruit #5"
android:key="listPref5"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #5 "/><ListPreference
android:title="Fruit #6"
android:key="listPref6"
android:defaultValue="digiGreen"
android:entries="@array/Fruits"
android:entryValues="@array/listValues" android:summary="Select Fruit #6 "/>
</PreferenceCategory>
</PreferenceScreen>
strings.xml
<string-array name="Fruits">
<item>Apple</item>
<item>Pear</item>
<item>Grape</item>
<item >Berry</item>
<item >Tomato</item>
</string-array>
<string-array name="listValues">
<item>apple</item>
<item>pear</item>
<item>grape</item>
<item>berry</item>
<item>tomato</item>
</string-array>
==============================
Found the solution
Options.java
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Options extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Getting the settings
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
for (int num = 1; num < 7; num++) {
String icon = sp.getString("listPref" + num, null);
try {
Class<drawable> res = R.drawable.class;
Field field = res.getField(icon);
PoliticiansArray[num] = field.getInt(null);
Class<string> stringRes = R.string.class;
Field stringField = stringRes.getField(icon);
SayingsArray[num] = stringField.getInt(null);
} catch (Exception e) {
Log.e("MyTag", "Failed", e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您应该将
PreferenceChangeListener
添加到您的ListPreference
这是如何从
ListPreference
获取新选择的值的示例:当您想要获取返回您在 SharedPreferences 上保存的值,如下所示:
然后,获取您的可绘制对象:
You should add a
PreferenceChangeListener
to yourListPreference
This is an example of how to get the newly selected value from a
ListPreference
:When you want to get back the value you saved on SharedPreferences, do it like this:
Then, get your drawable: