最近學習android時發現,很多書上都介紹了preference喜好設定這個東西,但是大部分的書都是直接上來講怎麼用,對其的用途和來曆都是隻字不提,筆者本人對於這種做法是非常鄙視的。
這裡,我將對其進行一點簡單的描述,可能說法並不嚴謹,但是至少能協助你理解到底什麼事喜好設定:
喜好設定這個詞是preference翻譯過來的,至於它到底是什麼,我用一句話概括下:preference是一種android為我們提供的方便的對資料進行儲存的工具。
分析這句話:
首先,我們明確,preference是和資料存放區相關的。
其次,它能協助我們方便的進行資料存放區!為什麼這個地方一定要強調下方便的這個詞呢?原因是,我們可以根本就不使用,我們有另外的N種辦法可以實現同樣的功能!它的出現,相當於為我們提供了一個方便的工具,當然了,這個工具並不是必須的。
preference都應用在什麼情境呢?
這得從android對preference的實現說起,實際上,preference所儲存的資料最後都會以xml檔案格式的形式進行儲存,而且其只能儲存一些基本格式的資料。例如string/boolean……。該xml檔案存放的位置在data/data/你應用的包名/shared_prefs檔案夾下。
種種的限制與實現機製表明了,preference非常適合於參數設定功能。實際上,它也確實是幹這個的,我們通過使用preference可以迅速的將某些值儲存進xml檔案中,然後我們可以讀取這些設定資訊進行相應的操作。
為了簡化與preference相關的應用開發,android為我們提供了一系列的api來協助我們。主要有PreferenceActivity,ListPreference,EditTextPreference,CheckBoxPreference,RingtonePreference
下面我們簡單的介紹下ListPreference的用法:
最終效果圖:
當我們點擊該選項時,會彈出這樣一個選擇頁面。
我們選擇了山東,然後該頁面就會自動關閉,並且和山東所對應的值也已經寫入了背景xml檔案中。
activity代碼:
package cn.com.chenzheng_java.pref; import android.os.Bundle; import android.preference.ListPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.util.Log; /** * @description 有關喜好設定preferences的研究 * @author chenzheng_java * @since 2011/03/29 * 繼承了PreferenceActivity我們可以方便的對preference進行操作。 * 例如可以通過getPreferenceManager擷取喜好設定管理器 * 那,我們可不可以不繼承PreferenceActivity呢?當然可以,你還記得不記得,實際上Activity類中 * 就有個SharedPreferences getSharedPreferences(String name, int mode)方法呢,我們通過它 * 也可以對preference進行操作。當然了,如果我們不繼承PreferenceActivity的話,那麼我們就要手動的 * 對資料進行儲存了。而不是跟現在一樣,會自動的根據你的選擇項進行資料儲存。 * 那麼,preference在這裡是怎麼樣進行自動儲存的呢,答案很簡單,那就是在addPreferencesFromResource方法的具體實現中! */ public class MyPreferencesActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.mylistpreference); /** * getPreferenceManager返回喜好設定管理器對象 */ PreferenceManager manager = getPreferenceManager(); // 根據android:key中指定的名稱(相當於id)來擷取喜好設定 ListPreference listPreference = (ListPreference) manager.findPreference("myListPreference"); Log.i("儲存的值為", ""+listPreference.getValue()); } }
res/xml/mylistperference.xml布局檔案
<?xml version="1.0" encoding="utf-8"?> <!-- 對於該檔案需要注意以下幾點 第一:位置。該檔案的位置是在res/xml/下的。 第二:格式,PreferenceScreen為根標籤,ListPreference為子標籤 第三:標籤屬性含義 android:key 唯一識別碼,和android:id相類似,PreferenceManager可以以其為參數通過findPreference擷取指定的preference android:title 整個螢幕的標題 android:summary 選項的簡單說明 android:entries 彈出的對話方塊中,列表顯示的常值內容,注意哦,這裡指定的是一個數組哦 android:entryValues 與android:entries相對應的值 android:defaultValue 當對應值不存在時的預設值 android:dialogTitle 彈出的對話方塊中的標題資訊 --> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="screen_list" android:title="標題" android:summary="說明摘要" > <ListPreference android:key="myListPreference" android:title="標題" android:summary="說明摘要" android:entries="@array/list_entries" android:entryValues="@array/list_entries_value" android:dialogTitle="dialogTitle" android:defaultValue="@array/list_entries_value2" ></ListPreference> </PreferenceScreen>
res/values/arrays.xml為我們的list提供了初始化資料哦
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="list_entries"> <item>山東</item> <item>福建</item> <item>北京</item> <item>河北</item> </string-array> <string-array name="list_entries_value"> <item>shandong1</item> <item>fujian1</item> <item>beijing1</item> <item>hebei1</item> </string-array> <string-array name="list_entries_value2"> <item>shandong2</item> <item>fujian2</item> <item>beijing2</item> <item>hebei2</item> </string-array> </resources>
當我們運行,並選擇了福建時,我們可以查看在shared_prefes下的xml檔案如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="myListPreference">fujian1</string></map>
我們可以看到,myListPreference就是我們指定的那個android:key的值,而fujian1就是我們在arrays中定義的某個值!
EditTextPreference:
效果圖:
當我們點擊首頁面的輸入名稱時,就會彈出該對話方塊,讓我們輸入名稱。
代碼:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="螢幕標題" android:summary="螢幕簡要說明" > <EditTextPreference android:dialogTitle="輸入您的名稱:" android:key="editTitlePreference" android:summary="簡要說明" android:title="輸入名稱" ></EditTextPreference> </PreferenceScreen>
RingtonePreference:
效果圖
代碼:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="螢幕標題" android:summary="螢幕簡要說明" > <!-- android:ringtoneType 設定響鈴模式,主要包括ringtone、notification、alarm、all android:showSilent 是否顯示靜音 注意,如果模擬器中沒有鈴聲的話,我們可以自己添加。將音樂複製到SD卡上,然後轉到android media player應用程式,選擇該音樂, 單擊menu,然後選擇 uses as ringtone --> <RingtonePreference android:key="ringtonePreference" android:summary="簡要說明" android:title="選擇系統鈴聲" android:ringtoneType="alarm" android:showSilent="true" ></RingtonePreference> </PreferenceScreen>
我們看看背景xml中是如何儲存的
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="ringtonePreference">content://settings/system/alarm_alert</string></map>
這裡我們可要注意了哦,ringtonePreference的值是一個uri字串。
組織喜好設定
何謂組織喜好設定啊,實際上就是為喜好設定分組!
分組之後,我們首頁只顯示組名,當我們點擊進去的時候,才會顯示具體的喜好設定列表。如圖:
代碼:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="螢幕標題" android:summary="螢幕簡要說明" > <!-- 第一組 --> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第一組" android:summary="點擊進入第一組喜好設定" > <RingtonePreference android:key="ringtonePreference" android:summary="簡要說明" android:title="選擇系統鈴聲" android:ringtoneType="alarm" android:showSilent="true" > </RingtonePreference> </PreferenceScreen> <!-- 第二組 --> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第二組" android:summary="點擊進入第二組喜好設定" > <EditTextPreference android:dialogTitle="輸入您的名稱:" android:key="editTitlePreference" android:summary="簡要說明" android:title="輸入名稱" ></EditTextPreference> </PreferenceScreen> <!-- 第三組 --> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第三組" android:summary="點擊進入第三組喜好設定" > <EditTextPreference android:dialogTitle="輸入您的名稱:" android:key="editTitlePreference" android:summary="簡要說明" android:title="輸入名稱" ></EditTextPreference> </PreferenceScreen> </PreferenceScreen>
以上的這種方法適合喜好設定的數目較多時使用。
如果我們喜好設定的數目較少,但是我們依舊想為他們分組下,怎麼辦呢?
我們可以將上面代碼中的嵌套PreferenceScreen改為PreferenceCategory,就這麼簡單!!!
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="螢幕標題" android:summary="螢幕簡要說明" > <!-- 第一組 --> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第一組" android:summary="點擊進入第一組喜好設定" > <RingtonePreference android:key="ringtonePreference" android:summary="簡要說明" android:title="選擇系統鈴聲" android:ringtoneType="alarm" android:showSilent="true" > </RingtonePreference> </PreferenceCategory> <!-- 第二組 --> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第二組" android:summary="點擊進入第二組喜好設定" > <EditTextPreference android:dialogTitle="輸入您的名稱:" android:key="editTitlePreference" android:summary="簡要說明" android:title="輸入名稱" ></EditTextPreference> </PreferenceCategory> <!-- 第三組 --> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:key="edittext_screen" android:title="第三組" android:summary="點擊進入第三組喜好設定" > <EditTextPreference android:dialogTitle="輸入您的名稱:" android:key="editTitlePreference" android:summary="簡要說明" android:title="輸入名稱" ></EditTextPreference> </PreferenceCategory> </PreferenceScreen>
效果圖可就大變樣了: