本文介绍了安卓:无法从settings.xml中获得preference值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在res文件夹一个Settings.xml以管理用户设置。我加载到一个片段这一点。我需要将这些设置保存到后端服务为了这个,我想知道已经什么值由用户或用户随时改变的设置我想知道,这样我更新有关设置的服务器。

I have a settings.xml in my res folder to manage user settings. I load this into a fragment. I require to store these settings to a backend service for this I want to know what values have been by the user or whenever user changes a setting I want to know so that I update the server about the settings.

我曾尝试,如下面的code,但我无法读出的值多种方法。我试图把面包和日志语句来检查监听器,但没有什么是出现表明听众是没有得到调用。难道我做错了什么?

I have tried many approaches as shown in the code below but I am unable to read the value. I tried putting Toast and log statements to check the listeners but nothing is appearing which indicates that listener is not getting called. Am I doing something wrong?

UserSettingFragment.java

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

settings.xml

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


     
        

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

</PreferenceCategory>

推荐答案

由于您使用的是 preferenceFragment 您需要注册和注销 OnShared preferenceChangeListener 方法 onResume 的onPause 方法#3 的,你在你的问题中提到。

Since you are using a PreferenceFragment you need to register and unregister OnSharedPreferenceChangeListener in the onResume and onPause methods for method #3 that you mentioned in your question.

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

<一个href=\"http://stackoverflow.com/questions/13596250/how-to-listen-for-$p$pference-changes-within-a-$p$pferencefragment\">Reference

这篇关于安卓:无法从settings.xml中获得preference值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:25