如何从屏幕上删除 Android 偏好设置
我正在尝试从屏幕上删除首选项,以便在用户使用高于 2.2 的 SDK 时该首选项不可见。我找到了几个答案,说 getPreferenceScreen().removePreference(thePreference) 可以工作,但每次我尝试它时都会返回 FALSE 。我是不是用错地方了?我的代码有什么线索吗?
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String POLLING_PREFERENCE = "update_frequency_list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// Get a reference to the preferences
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE);
//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead)
if(Build.VERSION.SDK_INT > 7) {
getPreferenceScreen().removePreference(mPollPref);
}
}
....
}
I'm trying to remove a preference from the screen so it isn't visible if the user is using an SDK greater than 2.2. I've found several answers saying that getPreferenceScreen().removePreference(thePreference) will work, but I'm getting FALSE returned every time I try it. Am I using it in the wrong place? Any clue from my code?
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String POLLING_PREFERENCE = "update_frequency_list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// Get a reference to the preferences
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE);
//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead)
if(Build.VERSION.SDK_INT > 7) {
getPreferenceScreen().removePreference(mPollPref);
}
}
....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我的情况是,我的偏好被分成了几类,当偏好被分类时,你不能像我们试图做的那样简单地“.removePreference”。我首先必须引用包含我想要删除的首选项的类别,然后像这样从该类别中删除该首选项。
我认为这与使用同一键拥有多个首选项的能力有关(以便删除正确的一个)。不管怎样,谢谢弗拉基米尔对我的支持。如果没有你,绝对不会找到答案。
Okay the issue in my case was that my preferences were separated into categories and when the preferences are categorized you can't simply ".removePreference" like we were trying to do. I first had to reference the category containing the preference I wanted to remove and then remove that preference from that category like so..
I assume this has something to do with the ability to have multiple preferences with the same key (in order to remove the correct one). Anyway thank you Vladimir for sticking with me. Definitely wouldn't have found the answer without you.
代码似乎是正确的。告诉你下面的代码可以工作,刚刚测试过:
}
你对 sdk 构建的检查怎么样?代码真的符合这个条件吗?
请检查您偏好的名称。可能您还没有通过
findPreference
方法获得它。Code seems to be right. Tell you that the following code works, just tested it:
}
What about your check for sdk build? Does the code really go under the condition?
And please, check the name of your preference. May be you haven't got it with
findPreference
method.