大家好,我們這一節講的是Android Preferences 的學習,Preferences 在Android當中被用來記錄應用,以及用戶喜好等等,它可以用來保存
簡單的數據類型,如Int,Double,Boolean等。Preferences中保存的數據可以理解為Map型。我們通過**PreferenceManager**以及`**getDefaultSharedPreferences(Context)**來獲取它,比如當我們想獲得整數我們可以用`**`getInt(String key, int defVal)`**.獲取里面的某個鍵值,當我們想修改時候我們用**`putInt(String key, int newVal),`**`最后用`**`edit(),`**`方法提交!千萬不要忘記了哦~`
?
為了讓大家跟好的理解我做了一個簡單的Demo,程序主要有個TextView控件,上面寫著用戶使用改應用的次數。效果如下圖所示:
?

?
下面是實現Demo的大體步驟:
?
一、新建一個Android工程命名為:PreferencesDemo。
?
二、在修改main.xml布局文件,這里只是在TextView控件里加了一個id.代碼如下:
?
~~~
<?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的代碼,全部代碼如下:
?
~~~
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 { /** Called when the activity is first created. */ @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(); }}
~~~
?
四、運行代碼,實現上述效果.
?
五、查看Preferences文件,首先打開命令終端:adb shell一下,然后cd data/data進入該目錄,ls一下我們會發現一大堆包文件,入下圖所示:
?

?
cd com.android.tutor(這里是我程序的包名)/shared_prefs,ls一下會發現.xml文件如下圖:
?

?
打開.xml文件,格式如下(為什么這樣大家自己去理解):
?
~~~
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><int name="counter" value="3" /></map>
~~~
?
OK,今天就到此為止,以上全是個人愚見,如果有什么地方不對的,請指正,謝謝大家!
- 前言
- (一)Android常用名令集錦(圖文并茂)!
- (二)Android Launcher抽屜類SlidingDrawer的使用!
- (三)Android 中自定義View的應用.
- (四)Android 中自定義屬性(attr.xml,TypedArray)的使用!
- (五)Android 中LayoutInflater的使用!
- (六)Android 中MenuInflater的使用(布局定義菜單)!
- (七)Android 中Preferences的使用!
- (八)Android Widget開發案例(世界杯倒計時!)
- (九)Android Handler的使用!!!
- (十)Android PopupWindow的使用!!!
- (十一)Android 通用獲取Ip的方法(判斷手機是否聯網的方法)!!!
- (十二)Android 在一個應用中如何啟動另外一個已安裝的應用!!!
- (十三)Android 數據庫SQLiteDatabase的使用!!
- (十四)Android Location的使用!!
- (十五)通過Location獲取Address的使用!
- (十六)Android中萬能的BaseAdapter(Spinner,ListView,GridView)的使用!
- Android 中的拿來主義(編譯,反編譯,AXMLPrinter2,smali,baksmali)!
- (十七)Android中Intent傳遞對象的兩種方法(Serializable,Parcelable)!
- (十八)列出Android設備中所有啟動的服務,及判斷某個服務是否開啟!
- (十九)Android開發中,使用線程應該注意的問題!
- (二十)Android與JavaScript方法相互調用!
- (二十一)Android中創建與幾種解析xml的方法!
- (二十二)Android中幾種圖像特效處理的集錦!!
- (二十三)Android中的日歷讀寫操作!!!
- (二十四)Android WebView的緩存!!!
- (二十五)Android 中的AIDL!!!