How to remove Android preferences from the screen

后端 未结 3 1410
花落未央
花落未央 2020-12-30 03:27

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 getPreferenceS

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 04:10

    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.

提交回复
热议问题