Android:无法从settings.xml获取首选项值

时间:2014-04-05 14:27:55

标签: android sharedpreferences preferences

我的res文件夹中有一个settings.xml来管理用户设置。我将其加载到片段中。我需要将这些设置存储到后端服务中,我想了解用户的值,或者每当用户更改我想知道的设置时,我都会更新服务器的设置。

我尝试了很多方法,如下面的代码所示,但我无法读取该值。我尝试使用Toast和log语句来检查侦听器,但没有出现任何表明侦听器未被调用的内容。我做错了吗?

UserSettingFragment.java

public class UserSettingFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);
    //Method 1
    PreferenceScreen shared = getPreferenceScreen();
    shared.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity().getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
            Log.d("preference", "change");
            return true;
        }
    });

    final SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

    Preference gender = (Preference) findPreference("gender");
    //Method 2
    // Get the custom preference
    Preference mypref = (Preference) findPreference("mypref");
    mypref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // TODO Auto-generated method stub
            String highScore = sharedPref.getString("gender", "Male");
            Toast.makeText(getActivity().getApplicationContext(), "Changed to " + highScore, Toast.LENGTH_LONG).show();
            Log.d("preference", "change");
            return false;
        }
    });
} //Method 3
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity().getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
}   

}

的settings.xml

<?xml version="1.0" encoding="utf-8"?>

              

    <SwitchPreference 
        android:key="gender"
        android:title="I am"
        android:switchTextOn="Female"
        android:switchTextOff="Male"
         />

</PreferenceCategory>

1 个答案:

答案 0 :(得分:1)

由于您使用的是PreferenceFragment,因此您需要在方法#3 OnSharedPreferenceChangeListeneronResume方法中注册和取消注册onPause你在问题中提到过。

public class UserSettingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

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

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

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Toast.makeText(getActivity().getApplicationContext(), "Changed key: " + key, Toast.LENGTH_LONG).show();
    }
}

Reference