Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 持久化技術之 SharedPreferences 存儲實例詳解

編輯:關於 Android 程式設計

本文通過實例講解 Android 持久化技術中的 SharedPreferences 存儲,供感興趣的讀者參考。

1、SharedPreferences 存儲要點

在介紹過文件讀寫之後,本文繼續說明另一種持久化方式 SharedPreferences:

(1)SharedPreferences 採用 key-value 的形式存儲,通過 key 即可找到對應的 value。
(2)支持多種數據類型,如字符串、整型、布爾型等,並有對應的存與取方法。
(3)獲取 SharedPreferences 對象有多種方式:Context 的 getSharedPreferences 方法、Activity 的 getPreferences 方法,以及 PreferenceManager 的 getDefaultSharedPreferences 方法。
(4)寫入時需先通過 SharedPreferences 對象獲取內部的 Editor 對象。
(5)默認存儲路徑為 /data/data/包名/shared_prefs/ 目錄。
(6)存儲文件為 XML 格式。

2、示例

場景:點擊「保存」按鈕存儲數據,點擊「恢復」按鈕把數據讀回並回填到界面。

(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Account:" /> <EditText android:id="@+id/account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Input your account here" android:ems="10" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password:" /> <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/login" android:layout_span="2" android:layout_height="wrap_content" android:text="save data" /> </TableRow> <TextView android:layout_width="wrap_content" android:layout_height="20dp" android:background="#ff0000" android:text="我是萬惡的分割線" android:textSize="20sp" android:gravity="center" /> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Account:" /> <EditText android:id="@+id/account2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password:" /> <EditText android:id="@+id/password2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/login2" android:layout_span="2" android:layout_height="wrap_content" android:text="restore data" /> </TableRow> </TableLayout> 

(2)MainActivity.java

 package com.example.testsharedpreferences; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * Android 持久化技術-----SharedPreferences存儲 * @author yy * */ public class MainActivity extends Activity { private EditText accountEdit; private EditText passwordEdit; private Button saveButton; private Button restoreButton; private SharedPreferences pref; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //存儲按鈕 saveButton = (Button) findViewById(R.id.login); //為存儲按鈕添加點擊事件 saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //獲取SharedPreferences對象 //第一個參數:文件名,沒有則新建。第二個參數:寫入模式-覆蓋 pref = getSharedPreferences("second", MODE_PRIVATE); //獲取SharedPreferences.Editor對象 editor = pref.edit(); //獲取輸入的賬號內容 accountEdit = (EditText) findViewById(R.id.account); String account = accountEdit.getText().toString(); //獲取輸入的密碼內容 passwordEdit = (EditText) findViewById(R.id.password); String password = passwordEdit.getText().toString(); //存儲用戶名和密碼 editor.putString("account", account); editor.putString("password", password); //提交 editor.commit(); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); } }); //獲取恢復按鈕對象 restoreButton = (Button) findViewById(R.id.login2); //添加事件 restoreButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //獲取SharedPreference對象 pref = getSharedPreferences("second", MODE_PRIVATE); //讀取內容 String account = pref.getString("account", "this is default value"); String password = pref.getString("password", "this is default value"); //設置到響應位置 EditText editText2 = (EditText)findViewById(R.id.account2); editText2.setText(account); EditText passwordText2 = (EditText) findViewById(R.id.password2); passwordText2.setText(password); Toast.makeText(getApplicationContext(), "恢復成功", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

3、結果

輸入內容後點擊「save data」,系統會生成名為 second.xml 的存儲文件:

文件內容如下:

運行效果圖:

 

可以看到,點擊「恢復」後,之前保存的賬號與密碼被正確讀取並回填。相比明文存儲,正式項目中若涉及密碼等敏感信息,建議配合加密處理,避免直接以明文落盤。

希望本文所述對大家的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved