Android如何从SharedPreference设置EditTextPreference的默认值? [英] Android how to set default value of EditTextPreference from SharedPreference?
问题描述
这次在同一个项目中我遇到了一个稍微具有挑战性的问题,在res / xml文件夹的settings.xml文件中:
This time in the same project I'm facing a slightly challenging issue where in settings.xml file in the res/xml folder:
<EditTextPreference
android:key="weight"
android:title="@string/update_user_weight"
android:persistent="true"
android:dialogTitle="@string/update_user_weight"
android:dialogMessage="@string/update_user_weight_message"
android:defaultValue="" />
<EditTextPreference
android:key="age"
android:title="@string/update_user_age"
android:persistent="true"
android:dialogTitle="@string/update_user_age"
android:dialogMessage="@string/update_user_age_message"
android:defaultValue="" />
以及类文件UserData.java:
and in a class file UserData.java:
SharedPreferences storeWeightAndAge = getSharedPreferences("WeightAndAgeStorage", Context.MODE_PRIVATE);
Editor store = storeWeightAndAge.edit();
store.putString("weight", weightData);
store.putString("age", ageData);
store.commit();
我在这里要做的是设置上面两个EditTextPreferences' android:defaultValue
在中存储
分别。权重
和年龄
SharedPreferences
What I'm trying to do here is to set the above two EditTextPreferences' android:defaultValue
to stored weight
and age
in the SharedPreferences
respectively.
现在,我该如何做呢?
编辑:提供了使用settings.xml文件的Settings.java文件:
provided Settings.java file that uses the settings.xml file:
package com.example.drinkup;
import android.content.SharedPreferences;
import android.os.*;
import android.preference.PreferenceFragment;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
推荐答案
好的,我知道了:
import android.preference.EditTextPreference;
public class Settings extends PreferenceActivity {
EditTextPreference weightEditTextPreference;
EditTextPreference ageEditTextPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
weightEditTextPreference = (EditTextPreference)findPreference("weight");
ageEditTextPreference = (EditTextPreference)findPreference("age");
SharedPreferences getWeightAndAgeStore = getSharedPreferences("weightAndAgeStorage", Context.MODE_PRIVATE);
weightEditTextPreference.setText(getWeightAndAgeStore.getString("weight", "0"));
ageEditTextPreference.setText(getWeightAndAgeStore.getString("age", "0"));
它的作用是将EditText框设置为 SharedPreferences
'保存密钥(在这种情况下,重量
和年龄
WeightAndAgeStorage
)按下时弹出的对话框中的数据。
What this does is that it sets the EditText box to the SharedPreferences
' saved key (in this case, weight
and age
of WeightAndAgeStorage
) data in the dialog that pops up when you press it.
希望其他人现在或将来阅读此内容(但是过去?)从这一发现中获益。
Hope anyone else reading this now or in the future (but what about the past?) benefits from this finding.
干杯!
这篇关于Android如何从SharedPreference设置EditTextPreference的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!