如何从屏幕上删除的Andr​​oid preferences [英] How to remove Android preferences from the screen

查看:214
本文介绍了如何从屏幕上删除的Andr​​oid preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从屏幕中删除preference所以它是不可见的,如果用户使用的SDK大于2.2。我已经发现了几个答案说得到preferenceScreen()。删除preference(在preference)的工作,但我越来越假回来每次我尝试它。我使用它错了地方?从我的code任何线索?

 公共类preferences扩展preferenceActivity实现OnShared preferenceChangeListener {

私有静态最后弦乐POLLING_ preFERENCE =update_frequency_list;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //从XML资源负载的preferences
    加preferencesFromResource(R.xml preferences。);

    //获取一个参考preferences
    。mPoll preF = GET preferenceScreen()找到preference(POLLING_ preFERENCE);

    //如果SDK是2.2或更高版本不显示轮询preferences(使用C2DM代替)
    如果(Build.VERSION.SDK_INT大于7){
        。GET preferenceScreen()删除preference(mPoll preF);
    }
}
....
}
 

解决方案

好这个问题在我的情况是,我的preferences分成类别,并在preferences被归类,不能简单地。删除preference像我们试图做的事情。我首先要引用包含preference我想删除,然后从该类别中,像这样删除preference类别。

 公共类preferences扩展preferenceActivity实现OnShared preferenceChangeListener {

私有静态最后弦乐POLLING_ preFERENCE =update_frequency_list;

@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);

//从XML资源负载的preferences
加preferencesFromResource(R.xml preferences。);

//获取一个参考preferences
。mPoll preF = GET preferenceScreen()找到preference(POLLING_ preFERENCE);

//如果SDK是2.2或更高版本不显示轮询preferences(使用C2DM代替)
如果(Build.VERSION.SDK_INT大于7){
    preferenceCategory notificationsCategory =(preferenceCategory)找到preference(notifications_category);
    notificationsCategory.remove preference(mPoll preF);
}
}
....
}
 

我想这已经是与有多个preferences使用相同的密钥(以去除正确的)的能力。不管怎样谢谢弗拉基米尔和我一起坚持。绝对不会找到答案,而无需你。

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

解决方案

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..

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) {  
    PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category");
    notificationsCategory.removePreference(mPollPref);
}  
}  
....  
}

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.

这篇关于如何从屏幕上删除的Andr​​oid preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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