Android 程式開發:(十七)喜好設定 —— 17.1 擷取預設資訊

來源:互聯網
上載者:User

下面展示如何使用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. 如果你把這個檔案開啟的話,會看見類似的內容。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.