歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android SharedPreferences的使用

Android SharedPreferences的使用

日期:2017/3/1 10:56:18   编辑: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. package com.cloay;
  2. import android.app.Activity;
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;
  8. import android.widget.EditText;
  9. import android.widget.ImageButton;
  10. /**
  11. *
  12. * MySharedPreferencesActivity.java
  13. * @author cloay
  14. * 2011-10-18
  15. */
  16. public class MySharedPreferencesActivity extends Activity {
  17. private EditText user = null;
  18. private EditText password = null;
  19. private ImageButton loginBtn = null;
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. user = (EditText)findViewById(R.id.user);
  25. password = (EditText)findViewById(R.id.pass);
  26. loginBtn = (ImageButton)findViewById(R.id.loginButton);
  27. initView();
  28. loginBtn.setOnTouchListener(new OnTouchListener(){
  29. @Override
  30. public boolean onTouch(View v, MotionEvent event) {
  31. if(event.getAction()==MotionEvent.ACTION_DOWN){
  32. v.setBackgroundResource(R.drawable.dengluxitong1);
  33. SharedPreferences userInfo = getSharedPreferences("user_info", 0);
  34. userInfo.edit().putString("name", user.getText().toString()).commit();
  35. userInfo.edit().putString("pass", password.getText().toString()).commit();
  36. }
  37. else if(event.getAction()==MotionEvent.ACTION_UP){
  38. v.setBackgroundResource(R.drawable.dengluxitong);
  39. }
  40. return false;
  41. }
  42. });
  43. }
  44. private void initView() {
  45. SharedPreferences userInfo = getSharedPreferences("user_info", 0);
  46. String username = userInfo.getString("name", "");
  47. String pass = userInfo.getString("pass", "");
  48. user.setText(username);
  49. password.setText(pass);
  50. }
  51. }
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
通過名稱,得到一個SharedPreferences,顧名思義,這個Preferences是共享的,共享的范圍據現在同一個Package中,這裡 面說所的Package和Java裡面的那個Package不同,貌似這裡面的Package是指在AndroidManifest.xml文件中的
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cloay"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="8" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".MySharedPreferencesActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. </manifest>
布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="185dp" android:id="@+id/user"
android:layout_height="40dp" android:hint="請輸入用戶名"
android:singleLine="true" android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/pass" android:layout_marginTop="66dp">
<requestFocus></requestFocus>
</EditText>
<EditText android:inputType="textPassword"
android:layout_width="185dp" android:id="@+id/pass"
android:layout_height="40dp" android:hint="請輸入密碼" android:singleLine="true"
android:layout_below="@+id/user" android:layout_centerHorizontal="true"
android:layout_marginTop="44dp">
</EditText>
<ImageButton android:layout_height="40dp"
android:layout_width="80dp" android:id="@+id/loginButton"
android:background="@drawable/dengluxitong"
android:layout_centerVertical="true" android:layout_alignRight="@+id/pass"
android:layout_marginRight="17dp"></ImageButton>
</RelativeLayout>

運行結果如下,首次顯示的時空白,第二次運行時如下:

Copyright © Linux教程網 All Rights Reserved