大家好,我們這一節(jié)講的是Android Preferences 的學(xué)習,Preferences 在Android當中被用來記錄應(yīng)用,以及用戶喜好等等,它可以用來保存
簡單的數(shù)據(jù)類型,如Int,Double,Boolean等。Preferences中保存的數(shù)據(jù)可以理解為Map型。我們通過PreferenceManager 以及getDefaultSharedPreferences(Context) 來獲取它,比如當我們想獲得整數(shù)我們可以用 getInt(String key, int defVal) .獲取里面的某個鍵值,當我們想修改時候我們用 putInt(String key, int newVal), 最后用 edit(), 方法提交!千萬不要忘記了哦~
為了讓大家跟好的理解我做了一個簡單的Demo,程序主要有個TextView控件,上面寫著用戶使用改應(yīng)用的次數(shù)。效果如下圖所示:
下面是實現(xiàn)Demo的大體步驟:
一、新建一個Android工程命名為:PreferencesDemo。
二、在修改main.xml布局文件,這里只是在TextView控件里加了一個id.代碼如下:
- view plaincopy to clipboardprint?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
三、修改PreferenceDemo.java的代碼,全部代碼如下:
- view plaincopy to clipboardprint?
- package com.android.tutor;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.widget.TextView;
- public class PreferencesDemo extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- SharedPreferences mPerferences = PreferenceManager
- .getDefaultSharedPreferences(this);
-
- int counter = mPerferences.getInt("counter", 0);
-
- TextView mTextView = (TextView)findViewById(R.id.text);
-
- mTextView.setText("This app has been started " + counter + " times.");
-
- SharedPreferences.Editor mEditor = mPerferences.edit();
-
- mEditor.putInt("counter", ++counter);
- mEditor.commit();
-
- }
- }
- package com.android.tutor;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.widget.TextView;
- public class PreferencesDemo extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- SharedPreferences mPerferences = PreferenceManager
- .getDefaultSharedPreferences(this);
-
- int counter = mPerferences.getInt("counter", 0);
-
- TextView mTextView = (TextView)findViewById(R.id.text);
-
- mTextView.setText("This app has been started " + counter + " times.");
-
- SharedPreferences.Editor mEditor = mPerferences.edit();
-
- mEditor.putInt("counter", ++counter);
- mEditor.commit();
-
- }
- }
四、運行代碼,實現(xiàn)上述效果.
五、查看Preferences文件,首先打開命令終端:adb shell一下,然后cd data/data進入該目錄,ls一下我們會發(fā)現(xiàn)一大堆包文件,入下圖所示:
cd com.android.tutor (這里是我程序的包名) /shared_prefs,ls一下會發(fā)現(xiàn).xml文件如下圖:
打開.xml文件,格式如下(為什么這樣大家自己去理解):
view plaincopy to clipboardprint?
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="counter" value="3" />
</map>
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="counter" value="3" />
</map>
OK,今天就到此為止,以上全是個人愚見,如果有什么地方不對的,請指正,謝謝大家!