在MainActivity中侦听SharedPreferences更改

时间:2016-07-04 08:57:53

标签: android android-preferences android-sharedpreferences

我正在尝试在SharedPreferences中收听MainActivity次更改。并更新应用程序设置中的值。工作代码如下:

private SharedPreferences SP, prefs;
SharedPreferences.OnSharedPreferenceChangeListener mListener;
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

mListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

        Toast.makeText(MainActivity.this, "Key changed: "+key, Toast.LENGTH_SHORT).show();
    }
};

SP.registerOnSharedPreferenceChangeListener(mListener);

但是当我尝试像这样改变键值时。它说不能解决findPreference方法。我尝试使用上下文,但仍然存在错误。

private SharedPreferences SP, prefs;
SharedPreferences.OnSharedPreferenceChangeListener mListener;
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

mListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

            if (key.equals("username")) {
                Preference pref = findPreference(key);
                pref.setDefaultValue(prefs.getString(key, "bob"));    }
};
SP.registerOnSharedPreferenceChangeListener(mListener);

我们如何在MainActivity中导入方法定义。请告诉我这里改变价值的方式,如果正确的话?

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

}

的preferences.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>

我基本上想要在代码中更改密钥“SharedPreferences”的username时更新设置页面中的首选项。

1 个答案:

答案 0 :(得分:2)

  

您必须在活动中实施PreferenceActivity

在您的活动名称前添加extends PreferenceActivity,然后导入其名称空间:

public class MainActivity extends PreferenceActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ...
   }
}

有关详细信息,请查看http://developer.android.com/reference/android/preference/PreferenceActivity.html

<强> EDITED

使用以下代码:

public class Settings extends PreferenceActivity  {

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

        SharedPreferences.OnSharedPreferenceChangeListener mListener;
        SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    }

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

        @Override
        public void onResume() {
            super.onResume();
            getPreferenceScreen().getSharedPreferences()
                    .registerOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onPause() {
            super.onPause();
            getPreferenceScreen().getSharedPreferences()
                    .unregisterOnSharedPreferenceChangeListener(this);
        }

        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            if (key.equals("username")) {
                Preference pref = findPreference(key);
                pref.setDefaultValue(prefs.getString(key, "bob"));
            }
        }
    }
}