写入哪个设置文件

写入哪个设置文件

本文介绍了PreferenceFragment读取/写入哪个设置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何控制PreferencesFragment应该使用哪个文件来读取和写入设置?我在文档中找不到任何有关此的信息.如果无法通过代码或XML资源控制该文件,则可以保证该文件的名称,因此我可以使用

How can I control which file should be used by a PreferencesFragment for reading and writing settings? I can't find anything about that in the docs. If that can't be controlled via code or XML resources, are there any guarantees, what the file is called, so I can open it explicitly using

Activity.getSharedPreferences(String name, int mode)

谢谢.

推荐答案

您必须操纵SettingsFragmentPreferenceManager.这就是它的样子

You have to manipulate the PreferenceManager of the SettingsFragment. This is what it looks like

// Constants
//--------------------------------------------------------------------------
private final static String TAG = SettingsFragment.class.getName();
public final static String SETTINGS_SHARED_PREFERENCES_FILE_NAME = TAG + ".SETTINGS_SHARED_PREFERENCES_FILE_NAME";

// Life-cycle
//--------------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate()");

    // Define the settings file to use by this settings fragment
    getPreferenceManager().setSharedPreferencesName(SETTINGS_SHARED_PREFERENCES_FILE_NAME);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
}

然后您可以像这样在片段之外访问此设置文件:

Then you can access this settings file outside of the fragment like this:

SharedPreferences preferences = getActivity().getSharedPreferences(
        SettingsFragment.SETTINGS_SHARED_PREFERENCES_FILE_NAME,
        Context.MODE_PRIVATE);

这篇关于PreferenceFragment读取/写入哪个设置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:30