在 PreferenceActivity 之外使用 findPreference(key)
我试图在与 PreferenceActivity 无关的活动之外使用此代码。我正在尝试这样做,以便我可以获得我的偏好的价值。
Preference prefNotify = findPreference(ACCUWX.Preferences.PREF_TEMPERATURE_NOTIFY);
String currentPrefValue = null;
if (prefNotify instanceof ListPreference) {
ListPreference listPref = (ListPreference) prefNotify;
currentPrefValue = (listPref.getEntry()).toString();
Log.i(DEBUG_TAG, "current pref value in done is " + currentPrefValue);
}
但我无法在类中访问 findPreference,因为它没有扩展 PreferenceActivity。我尝试实例化 PreferenceManager 对象,但也无法做到这一点。
I am trying to utilize this code outside of an activity not associated with a PreferenceActivity. I am trying this so that I can access the value of my preference.
Preference prefNotify = findPreference(ACCUWX.Preferences.PREF_TEMPERATURE_NOTIFY);
String currentPrefValue = null;
if (prefNotify instanceof ListPreference) {
ListPreference listPref = (ListPreference) prefNotify;
currentPrefValue = (listPref.getEntry()).toString();
Log.i(DEBUG_TAG, "current pref value in done is " + currentPrefValue);
}
but I can't access findPreference in my class because it doesn't extend PreferenceActivity. I tried to instantiate a PreferenceManager object, but can't do that either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加到 thinksteep 的答案中,您应该从您的 Activity: 中执行类似的操作:
或类似的操作。
Adding to thinksteep's answer, you should do something like this from your Activity:
or similar.
我认为您不需要实例化 PreferenceManager,我认为您只需执行
PreferenceManager.findPreference(key);
我现在无法验证这一点,但这里有一个包含类似讨论的链接PreferenceManagerI think you don't need to instantiate PreferenceManager, I think you can just do
PreferenceManager.findPreference(key);
I couldn't validate this right now, but here is a link with similar discussion PreferenceManager