Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android中刪除Preference詳解

Android中刪除Preference詳解

編輯:Android開發實例

Android的設置界面實現比較簡單,有時甚至只需要使用一個簡單的xml文件即可.聲明簡單,但是如何從PreferenceScreen或者PreferenceCategory中刪除一個Preference會簡單麼.為什麼有些人寫的就無法刪除成功呢?本文將從Android源碼實現來分析一下.

聲明文件

代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="root">

  <PreferenceCategory
      android:key="theme"
      android:title="Theme"
      android:summary="Theme Settings"
      >
      <CheckBoxPreference
          android:key="holo_theme"
          android:title="Holo Theme"
          android:summary="Use Holo Theme"
          />
     
  </PreferenceCategory>
 
  <CheckBoxPreference
      android:key="rmcache"
      android:title="Auto Clear Cache"
      android:summary="Enable Auto Clear Cache "
      />
</PreferenceScreen>

層級關系

刪除Preference

刪除key為rmcache的Preference,這個Preference是PreferenceScreen root的子節點.

代碼如下:

PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference autoClearCheckboxPref = (CheckBoxPreference) screen.findPreference("rmcache");
screen.removePreference(autoClearCheckboxPref);

刪除key為holo_theme的Preference,其為PreferenceScreen root的孫子節點,非直接關系.

代碼如下:

PreferenceCategory themePrefCategory = (PreferenceCategory) screen.findPreference("theme");
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)themePrefCategory.findPreference("holo_theme");
themePrefCategory.removePreference(holoCheckboxPref);

為什麼刪除失敗

很多人出現了刪除失敗的問題,主要原因是使用了非父親節點來刪除,比如這樣
代碼如下:

PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)screen.findPreference("holo_theme");
screen.removePreference(holoCheckboxPref);

PreferenceGroup刪除實現,其實PreferenceScreen和PreferenceCategory都是PreferenceGroup的子類.

代碼如下:

/**
     * Removes a {@link Preference} from this group.
     *
     * @param preference The preference to remove.
     * @return Whether the preference was found and removed.
     */
    public boolean removePreference(Preference preference) {
        final boolean returnValue = removePreferenceInt(preference);
        notifyHierarchyChanged();
        return returnValue;
    }

    private boolean removePreferenceInt(Preference preference) {
        synchronized(this) {
            preference.onPrepareForRemoval();
            return mPreferenceList.remove(preference);
        }
    }

而mPreferenceList中存放的都是當前PreferenceGroup的直接子Preference.

findPreference實現

findPreference查找不僅僅限於直接子Preference,會遍歷其所有的子Preference.

所以代碼中同樣有root PreferenceGroup和直接父PreferenceGroup引用時,通常後者效率會高.

代碼如下:

/**
 * Finds a {@link Preference} based on its key. If two {@link Preference}
 * share the same key (not recommended), the first to appear will be
 * returned (to retrieve the other preference with the same key, call this
 * method on the first preference). If this preference has the key, it will
 * not be returned.
 * <p>
 * This will recursively search for the preference into children that are
 * also {@link PreferenceGroup PreferenceGroups}.
 *
 * @param key The key of the preference to retrieve.
 * @return The {@link Preference} with the key, or null.
 */
public Preference findPreference(CharSequence key) {
    if (TextUtils.equals(getKey(), key)) {
        return this;
    }
    final int preferenceCount = getPreferenceCount();
    for (int i = 0; i < preferenceCount; i++) {
        final Preference preference = getPreference(i);
        final String curKey = preference.getKey();

        if (curKey != null && curKey.equals(key)) {
            return preference;
        }

        if (preference instanceof PreferenceGroup) {
            final Preference returnedPreference = ((PreferenceGroup)preference)
                    .findPreference(key);
            if (returnedPreference != null) {
                return returnedPreference;
            }
        }
    }

    return null;
}

findPreference和removePreference實現比較

為什麼findPreference遍歷所有的子節點,而removePreference不會,只會刪除直接子Preference

原因有以下幾點:

1.findPreference支持遍歷查找,減少了聲明諸多的中間PreferenceGroup代碼.而findPreference屬於常用接口方法.
2.removePreference調用較少.
3.當存在key相同的Preference時,如果removePreference不限定直接子Preference,那麼無法准確刪除哪一個.

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved