Android how to set default value of EditTextPreference from SharedPreference?

馋奶兔 提交于 2019-12-22 06:36:54

问题


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="" />

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();

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.

Now, how do I go about doing that?

EDIT: 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);



    }

}

回答1:


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.

Hope anyone else reading this now or in the future (but what about the past?) benefits from this finding.

Cheers!




回答2:


Firstly, set the default value for EditTextPreference. The value will be stored as a String type. For example:

<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:

PreferenceManager.setDefaultValue(this, R.xml.settings, false);



回答3:


All you have to do is use the sharedPreferences.getString() and then use the editext.setText() to that.



来源:https://stackoverflow.com/questions/28383400/android-how-to-set-default-value-of-edittextpreference-from-sharedpreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!