下面展示如何使用SharedPreferences對象去儲存應用的資料。你也將會看見通過特殊的Activity去修改已經被儲存的應用資料。
1. 建立一個工程,UsingPreferences。
2. 在res檔案夾下面建立一個檔案夾,xml。在建立的檔案夾中建立一個檔案,myappreferences.xml。
3. 在myapppreferences.xml檔案中編寫代碼。
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Category 1"> <CheckBoxPreference android:title="Checkbox" android:defaultValue="false" android:summary="True or False" android:key="checkboxPref" /> </PreferenceCategory> <PreferenceCategory android:title="Category 2"> <EditTextPreference android:summary="Enter a string" android:defaultValue="[Enter a string here]" android:title="Edit Text" android:key="editTextPref" /> <RingtonePreference android:summary="Select a ringtone" android:title="Ringtones" android:key="ringtonePref" /> <PreferenceScreen android:title="Second Preference Screen" android:summary= "Click here to go to the second Preference Screen" android:key="secondPrefScreenPref" > <EditTextPreference android:summary="Enter a string" android:title="Edit Text (second Screen)" android:key="secondEditTextPref" /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
4. 在包路徑下面建立一個類,AppPreferenceActivity。
5. AppPreferenceActivity中的代碼。
public class AppPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager prefMgr = getPreferenceManager(); prefMgr.setSharedPreferencesName("appPreferences"); //---load the preferences from an XML file--- addPreferencesFromResource(R.xml.myapppreferences); }}
6. 在AndroidManifest.xml中添加AppPreferenceActivity類的入口。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.manoel.UsingPreferences" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".UsingPreferencesActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AppPreferenceActivity" android:label="@string/app_name"> <intent-filter> <action android:name="net.manoel.AppPreferenceActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application></manifest>
7. main.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><Buttonandroid:id="@+id/btnPreferences"android:text="Load Preferences Screen"android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickLoad"/><Buttonandroid:id="@+id/btnDisplayValues"android:text="Display Preferences Values"android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickDisplay"/><EditText android:id="@+id/txtString" android:layout_width="fill_parent" android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btnModifyValues"android:text="Modify Preferences Values"android:layout_width="fill_parent" android:layout_height="wrap_content"android:onClick="onClickModify"/> </LinearLayout>
8. 在UsingPreferencesActivity.java中添加一些代碼。
public class UsingPreferencesActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void onClickLoad(View view) {Intent i = new Intent("net.manoel.AppPreferenceActivity");startActivity(i);}}
9. 按F11在模擬器上面測試。點擊最上面的按鈕,跳轉到UsingPreferences這個介面。
10. 點擊CheckBox,讓它的狀態在checked和unchecked之間進行切換。觀察CATEGORY 1和CATEGORY 2。點擊Edit Text,輸入一些字元,然後按OK把對話方塊消失掉。
11. 點擊Ringtones,選擇預設的鈴聲或靜音。如果在真機上面測試的話,可能會有更多的選項。
12. 點擊Second Preference Screen,就會跳轉到下一屏。
13. 如果想要返回上一屏,可以按Back鍵。
14. 一旦你修改了選項中的任何一項,在/data/data/net.manoel.UsingPreferences/shared_prefs檔案夾下面就會建立一個檔案。可以使用DDMS進行查看。
15. 如果你把這個檔案開啟的話,會看見類似的內容。