共享preferences和preferenceFragment [英] SharedPreferences and PreferenceFragment

查看:140
本文介绍了共享preferences和preferenceFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了preferenceFragments和共享preferences Android开发者网站上的例子,但是我运行到一个问题了。我希望能够从不同的活动访问我的设置屏幕中的preferences但我不知道什么是我的preference片段产生的preference文件的名称是。有谁知道有关指定preference文件名的preferenceFragment什么?

I have done the examples for PreferenceFragments and SharedPreferences on the ANdroid developer website, however I'm running into an issue now. I want to be able to access the preferences in my "Settings" screen from different Activities but I don't know what the name of the preference file generated by my preference fragment is. Does anyone know anything about specifying the preference filename for a PreferenceFragment?

推荐答案

在preferences应保存到默认的文件。

The Preferences should be saved to the default file.

要访问,请使用<一个href=\"https://developer.android.com/reference/android/$p$pference/$p$pferenceManager.html#getDefaultShared$p$pferences%28android.content.Context%29\"><$c$c>$p$pferenceManager#getDefaultShared$p$pferences()

To access, use PreferenceManager#getDefaultSharedPreferences()

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (context);

背景是任何有效的背景下,包括活动。

context is any valid Context, including Activities.

从上<一个文档href=\"http://developer.android.com/reference/android/$p$pference/$p$pferenceFragment.html\"><$c$c>$p$pferenceFragment:

要检索的共享preferences的一个实例是,preference
  层次结构这个片段将使用,通话
  getDefaultShared preferences(android.content.Context)与上下文
  相同的包,因为这片段。

To retrieve an instance of SharedPreferences that the preference hierarchy in this fragment will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this fragment.

这篇关于共享preferences和preferenceFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆