开发者

How to remove PreferenceCategory programmatically?

开发者 https://www.devze.com 2023-02-23 00:29 出处:网络
I need to remove a PreferenceCategory programmatically. I could remove the individual preferences with the following code but I need to remove (disable) w开发者_如何学编程hole PreferenceCategory as we

I need to remove a PreferenceCategory programmatically. I could remove the individual preferences with the following code but I need to remove (disable) w开发者_如何学编程hole PreferenceCategory as well.

PreferenceScreen preferenceScreen = getPreferenceScreen();
EditTextPreference etp = (EditTextPreference) preferenceScreen.findPreference("pref22");
((PreferenceGroup) findPreference("prefcat")).removePreference(etp);

Edit: Here's the working code for a PreferenceCategory "prefcat" and a child preference "pref22":

PreferenceScreen preferenceScreen = getPreferenceScreen();
EditTextPreference etp = (EditTextPreference) preferenceScreen.findPreference("pref22");

PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("prefcat");
if (preferenceGroup != null) {
    preferenceGroup.removePreference(etp);
    preferenceScreen.removePreference(preferenceGroup);
}


you can hide a category by getting reference to PreferenceScreen:

I your xml :

<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:key="@string/preferenceScreen">

//set all you values
//Preference, PreferenceCategory and/or CheckBoxPreference

</PreferenceScreen>

in you string.xml : don't forgot to set this new string

 <string name="preferenceScreen" translatable="false">preferenceScreen</string>

in you code:

preferenceScreen = (PreferenceScreen) findPreference(getResources().getString(R.string.preferenceScreen));

and then remove the category from your PreferenceScreen :

myCategory = (PreferenceCategory) findPreference(getResources().getString(R.string.my_category));
myPreferenceScreen.removePreference(myCategory);


Don't load the PreferenceCategory in the first place.

If you are defining your preferences in Java, don't create the PreferenceCategory.

If you are defining your preferences in XML, use three XML files:

  1. One for stuff before this magic category
  2. One for the magic category
  3. One for stuff after this magic category

In situations where you want the category, load all three XML files. In situations where you do not want the category, load only the first and third XML files.


Provide a key for your PreferenceScreen and your PreferenceCategory in XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="preferenceScreen" >

  <PreferenceCategory
     android:summary="@string/settings_billing_summary"
     android:title="Title"
     android:key="myPrefCat" >
      <Preference
      android:key="someKey"
      android:summary="Sum"
      android:title="Title" />
   </PreferenceCategory>

</PreferenceScreen>

From your class, you can now refer to your preferenceScreen and preferenceCategory and use the removePreference() method to remove the preference from the screen:

    PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("preferenceScreen");
    PreferenceCategory myPrefCat = (PreferenceCategory) findPreference("myPrefCat");
    preferenceScreen.removePreference(myPrefCat);

This answer is based on douarbou's answer which appears to be outdated, but is basically the same.


You can find the entire child-parent tree by traversing over all of the preferences and then check which is the parent of any preference you wish, even without using the id of the parent:

public static Map<Preference,PreferenceGroup> buildPreferenceParentTree(final PreferenceActivity activity)
    {
    final Map<Preference,PreferenceGroup> result=new HashMap<Preference,PreferenceGroup>();
    final Stack<PreferenceGroup> curParents=new Stack<PreferenceGroup>();
    curParents.add(activity.getPreferenceScreen());
    while(!curParents.isEmpty())
      {
      final PreferenceGroup parent=curParents.pop();
      final int childCount=parent.getPreferenceCount();
      for(int i=0;i<childCount;++i)
        {
        final Preference child=parent.getPreference(i);
        result.put(child,parent);
        if(child instanceof PreferenceGroup)
          curParents.push((PreferenceGroup)child);
        }
      }
    return result;
    }

sample usage:

  final Map<Preference,PreferenceGroup> preferenceParentTree=buildPreferenceParentTree(SettingsActivity.this);
  final PreferenceGroup preferenceGroup=preferenceParentTree.get(preferenceToRemove);
  preferenceGroup.removePreference(preferenceToRemove);

EDIT: seems there is a new API for this :

https://developer.android.com/reference/androidx/preference/Preference#setVisible(boolean)

I'm not sure if currently it's available or not, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消