使用共享首选项在android中设置首选项

时间:2016-07-04 04:14:37

标签: android android-preferences android-sharedpreferences

我尝试为测试应用构建设置页面。在代码中,我使用的是PreferenceActivity类。当我从设置页面设​​置一些设置时,我可以使用以下方法在应用程序的任何位置检索该值。

SharedPreferences SP;
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String username = SP.getString("username", "bob");

此外,我可以在代码中的任何位置设置键值。与提供上下文后的以下方法类似。

SharedPreferences.Editor edit;
edit.putString("username", "bob");
edit.commit();

问题是,当我打开设置页面时,"用户名"字段未使用" bob"更新。我想在设置Activity中反映SharedPreferences变量的更改值,这是对preferences.xml工作的扩展PreferenceActivity。我正在填写如下的偏好。

Settings.java

import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.os.Bundle;

public class Settings extends PreferenceActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        PrefManager prefManager;
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }

}

preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username">
    </EditTextPreference>


</PreferenceScreen>

3 个答案:

答案 0 :(得分:0)

我很困惑为什么你使用我的不同方法,尝试这样:

//get value
sp = getSharedPreferences("filename",MODE_PRIVATE);
sp.getString("username","bob");

//save value
sp = getSharedPreferences("filename",MODE_PRIVATE);
Editor edit= sp.edit();
edit.putString("username", "bob");
edit.commit();

答案 1 :(得分:0)

public class PrefsActivity extends PreferenceActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment {
        PreferenceManager prefManager;

        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.prefs);
            final Preference pref = findPreference("username");
            pref.setSummary("your value which you alradey saved in preferences");
            pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    // do whatever you want with new value

                    // true to update the state of the Preference with the new value
                    // in case you want to disallow the change return false

                    pref.setSummary(newValue.toString());
                    return true;
                }
            });
        }


    }

}

答案 2 :(得分:0)

使用旅行车(https://github.com/beplaya/Wagon)。用于共享偏好的超级超简单注释。

...
@Crate(key = "myPreferenceCrate", preference = true)
public CrateExample crate;
@WoodBox(key = "myPreferenceString", preference = true)
public String value;
...
wagon.pack(getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE));
...
wagon.unpack(getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE));
...