歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之SharedPreferes

Android應用開發之SharedPreferes

日期:2017/3/1 10:44:33   编辑:Linux編程

SharedPreferences是Android平台上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態,一般在Activity中 重載窗口狀態onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常規的Long長 整形、Int整形、String字符串型的保存,它是什麼樣的處理方式呢?SharedPreferences類似過去Windows系統上的ini配置文件,但是它分為多種權限,可以全局共享訪問,android123提示最 終是以xml方式來保存,整體效率來看不是特別的高,對於常規的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義文件格式。xml 處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對於內存資源占用比較好。

這種方式應該是用起來最簡單的Android讀寫外部數據的方法了。他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一樣,以一種簡單、透明的方式來保存一些用戶個性化設置的字體、顏色、位置等參數信息。一般的應用程序都會提供“設置”或者“首選項”的這樣的界面,那麼這些設置最後就可以通過Preferences來保存,而程序員不需要知道它到底以什麼形式保存的,保存在了什麼地方。當然,如果你願意保存其他的東西,也沒有什麼限制。只是在性能上不知道會有什麼問題。

在Android系統中,這些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目錄下。

  1. SharedPreferencespre = getSharedPreferences("soft",
  2. Context.MODE_WORLD_READABLE);

在這裡我們可以調用 activity 為我們提供的方法,這個方法有兩個參數:

1). 文件名。 在這裡要特別注意。 因為在Android 中已經確定了SharedPreferences 是以 xm l形式保存,所以,在填寫文件名參數時,不要給定 ” .xml ”後綴, android 會自動添加 。它是采用鍵值對的形式保存參數。 當你需要獲得某個參數值時, 按照參數的鍵索引即可。

2). 第二個可以理解為創建模式和之前的文件存儲的模式是一樣的。

Context. MODE_PRIVATE

Context.MODE_APPEND MODE_APPEND

Context.MODE_WORLD_READABLE

Context.MODE_WORLD_WRITEABLE

實驗步驟

1、 資源

  1. <string name="name_text">姓名</string>
  2. <string name="age_text">年齡</string>
  3. <string name="save_text">提交</string>
2、 布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginRight="30dp"
  14. android:text="@string/name_text" />
  15. <EditText
  16. android:id="@+id/nameEt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal" >
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_marginRight="30dp"
  28. android:text="@string/age_text" />
  29. <EditText
  30. android:id="@+id/ageEt"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content" />
  33. </LinearLayout>
  34. <Button
  35. android:id="@+id/saveBtn"
  36. android:layout_marginTop="30dp"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:text="@string/save_text" />
  40. </LinearLayout>

3、 實現保存

  1. private void findViews() {
  2. nameEt = (EditText) this.findViewById(R.id.nameEt);
  3. ageEt = (EditText) this.findViewById(R.id.ageEt);
  4. saveBtn = (Button) this.findViewById(R.id.saveBtn);
  5. saveBtn.setOnClickListener(new View.OnClickListener() {
  6. public void onClick(View v) {
  7. String name = nameEt.getText().toString().trim();
  8. int age =
  9. Integer.valueOf(ageEt.getText().toString().trim());
  10. SharedPreferences sharedPreferences =
  11. SharedpreferencesTestActivity.this
  12. .getSharedPreferences("myOption", MODE_PRIVATE);
  13. Editor editor = sharedPreferences.edit();
  14. editor.putString("name" ,name);
  15. editor.putInt("age", age);
  16. editor.commit();
  17. }
  18. });
  19. }
Copyright © Linux教程網 All Rights Reserved