Android中刪除Preference詳解
Android的設(shè)置界面實(shí)現(xiàn)比較簡(jiǎn)單,有時(shí)甚至只需要使用一個(gè)簡(jiǎn)單的xml文件即可.聲明簡(jiǎn)單,但是如何從PreferenceScreen或者PreferenceCategory中刪除一個(gè)Preference會(huì)簡(jiǎn)單么.為什么有些人寫的就無(wú)法刪除成功呢?本文將從Android源碼實(shí)現(xiàn)來(lái)分析一下.
聲明文件
<?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>
層級(jí)關(guān)系

刪除Preference
刪除key為rmcache的Preference,這個(gè)Preference是PreferenceScreen root的子節(jié)點(diǎn).
PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference autoClearCheckboxPref = (CheckBoxPreference) screen.findPreference("rmcache");
screen.removePreference(autoClearCheckboxPref);
刪除key為holo_theme的Preference,其為PreferenceScreen root的孫子節(jié)點(diǎn),非直接關(guān)系.
PreferenceCategory themePrefCategory = (PreferenceCategory) screen.findPreference("theme");
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)themePrefCategory.findPreference("holo_theme");
themePrefCategory.removePreference(holoCheckboxPref);
為什么刪除失敗
很多人出現(xiàn)了刪除失敗的問(wèn)題,主要原因是使用了非父親節(jié)點(diǎn)來(lái)刪除,比如這樣
PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)screen.findPreference("holo_theme");
screen.removePreference(holoCheckboxPref);
PreferenceGroup刪除實(shí)現(xiàn),其實(shí)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中存放的都是當(dāng)前PreferenceGroup的直接子Preference.
findPreference實(shí)現(xiàn)
findPreference查找不僅僅限于直接子Preference,會(huì)遍歷其所有的子Preference.
所以代碼中同樣有root PreferenceGroup和直接父PreferenceGroup引用時(shí),通常后者效率會(huì)高.
/**
* 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實(shí)現(xiàn)比較
為什么findPreference遍歷所有的子節(jié)點(diǎn),而removePreference不會(huì),只會(huì)刪除直接子Preference
原因有以下幾點(diǎn):
1.findPreference支持遍歷查找,減少了聲明諸多的中間PreferenceGroup代碼.而findPreference屬于常用接口方法.
2.removePreference調(diào)用較少.
3.當(dāng)存在key相同的Preference時(shí),如果removePreference不限定直接子Preference,那么無(wú)法準(zhǔn)確刪除哪一個(gè).
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
- Android實(shí)現(xiàn)WebView刪除緩存的方法
- Android Studio徹底刪除項(xiàng)目 Android Studio徹底刪除Module
- Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
- Android高仿QQ6.0側(cè)滑刪除實(shí)例代碼
- Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法
- Android中刪除文件以及文件夾的命令記錄
- Android遞歸方式刪除某文件夾下的所有文件(.mp3文件等等)
- android實(shí)現(xiàn)簡(jiǎn)單左滑刪除控件
相關(guān)文章
21天學(xué)習(xí)android開發(fā)教程之MediaPlayer
21天學(xué)習(xí)android開發(fā)教程之MediaPlayer,MediaPlayer可以播放音頻和視頻,操作相對(duì)簡(jiǎn)單,感興趣的小伙伴們可以參考一下2016-02-02
Android編程單選項(xiàng)框RadioGroup綜合應(yīng)用示例
這篇文章主要介紹了Android編程單選項(xiàng)框RadioGroup用法,結(jié)合實(shí)例形式分析了Android單選按鈕組RadioGroup的定義與具體使用技巧,需要的朋友可以參考下2016-10-10
Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解
這篇文章主要為大家介紹了Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決
這篇文章主要為大家介紹了Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android實(shí)現(xiàn)后臺(tái)開啟服務(wù)默默拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)后臺(tái)開啟服務(wù)默默拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android MPAndroidChart開源庫(kù)圖表之折線圖的實(shí)例代碼
這篇文章主要介紹了Android MPAndroidChart開源庫(kù)圖表之折線圖的實(shí)例代碼,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09

