0
我想顯示選擇的水果作爲繪製 我有它,這樣可以節省,但我不知道如何從行動嗜好 得到它,使用它的Android得sharedPreferences存儲用戶選擇
我認爲可能不會停,如水果= 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);
}
確定最後一件事什麼是SharedPreferences設置爲零= getSharedPreferences(「Fruits」,0);它是通過0移動到5嗎?我是7天前開始的Android編碼新手 – zeitue
這是關於權限,請查看這裏:http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String, %20int%29 然而,如果你想多個水果選擇....你應該使用幾個ListPreference的...而不是一個... – MobileCushion
我將示例代碼添加到我的程序,並將listPref更改爲listPref1 Fruits to Fruit1所有其他設置仍然保存爲什麼這不僅應該保存第一個? – zeitue