java Android 如何从 SharedPreference 设置 EditTextPreference 的默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28383400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android how to set default value of EditTextPreference from SharedPreference?
提问by blaberer
This time in the same project I'm facing a slightly challenging issue where in settings.xml file in the res/xml folder:
这次在同一个项目中,我在 res/xml 文件夹中的 settings.xml 文件中遇到了一个稍微具有挑战性的问题:
<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="" />
and in a class file UserData.java:
在类文件 UserData.java 中:
SharedPreferences storeWeightAndAge = getSharedPreferences("WeightAndAgeStorage", Context.MODE_PRIVATE);
Editor store = storeWeightAndAge.edit();
store.putString("weight", weightData);
store.putString("age", ageData);
store.commit();
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.
我在这里尝试做的是将上述两个 EditTextPreferences' 分别设置android:defaultValue
为存储weight
和age
中SharedPreferences
。
Now, how do I go about doing that?
现在,我该怎么做呢?
EDIT: provided Settings.java file that uses the settings.xml file:
编辑:提供了使用 settings.xml 文件的 Settings.java 文件:
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);
}
}
采纳答案by blaberer
Okay guys, I got it:
好的,伙计们,我明白了:
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"));
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.
这里做的事情是,它设置的EditText框的SharedPreferences
“保存键(在这种情况下,weight
和age
的WeightAndAgeStorage
)数据,当您按下弹出在对话框中。
Hope anyone else reading this now or in the future (but what about the past?) benefits from this finding.
希望现在或将来阅读本文的其他人(但过去呢?)从这一发现中受益。
Cheers!
干杯!
回答by Anggrayudi H
Firstly, set the default value for EditTextPreference
. The value will be stored as a String
type. For example:
首先,设置默认值EditTextPreference
。该值将存储为String
类型。例如:
<EditTextPreference
android:key="weight"
android:defaultValue="enter your value (54)"
... />
<EditTextPreference
android:key="age"
android:defaultValue="enter your value"
... />
Then, apply the default value in activity where your app's activity is started first (once installed for first). For example, in the MainActivity
's onCreate()
method:
然后,在首先启动应用程序活动的活动中应用默认值(首先安装后)。例如,在MainActivity
的onCreate()
方法中:
PreferenceManager.setDefaultValue(this, R.xml.settings, false);
回答by endlesschaos
All you have to do is use the sharedPreferences.getString() and then use the editext.setText() to that.
您所要做的就是使用 sharedPreferences.getString() 然后使用 editext.setText() 。