在Android應用中,我們常需要記錄使用者佈建的一些偏好參數,,此時我們就需要用SharedPreferences和Editor將這些資訊儲存下來,在下次登入時讀取。
SharedPreferences儲存的資料主要類似於配置資訊格式的資料,因此它儲存資料的形式為key-value對,下面我們來看下執行個體代碼。
首先是介面布局,比較簡單,就是一個普通的登陸介面.
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" >10 <EditText 11 android:layout_width="fill_parent"12 android:layout_height="wrap_content"13 android:id="@+id/account"14 />15 <EditText 16 android:layout_width="fill_parent"17 android:layout_height="wrap_content"18 android:id="@+id/password"19 android:layout_below="@id/account"20 />21 <Button 22 android:layout_width="fill_parent"23 android:layout_height="wrap_content"24 android:layout_below="@id/password"25 android:text="儲存參數"26 android:id="@+id/save"27 android:onClick="save"28 />29 </RelativeLayout>
這是自訂的Preferences 類,用來實現資料的儲存 ,可在Android的內建儲存空間產生一檔案。
1 import android.R.integer; 2 import android.content.Context; 3 import android.content.SharedPreferences; 4 import android.content.SharedPreferences.Editor; 5 import android.widget.EditText; 6 7 public class Preferences { 8 9 private Context context;10 public Preferences(Context context)11 {12 this.context=context;13 }14 15 16 public void save(String name, Integer valueOf) 17 {18 //儲存檔案名稱字為"shared",儲存形式為Context.MODE_PRIVATE即該資料只能被本應用讀取19 SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE);20 21 Editor editor=preferences.edit();22 editor.putString("name", name);23 editor.putInt("age", valueOf);24 25 editor.commit();//提交資料26 }27 28 29 }
下面是Mainactivity的代碼。在activity的oncreate階段我們載入本地的資料。
import java.util.HashMap;import java.util.Map;import android.R.integer;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText account,passworad; Preferences prefer;//自訂的類 SharedPreferences preference;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); account=(EditText)findViewById(R.id.account); passworad=(EditText)findViewById(R.id.password); //擷取本地的資料 preference=getSharedPreferences("shared", MODE_PRIVATE); Map<String, String> map=new HashMap<String, String>(); map.put("name",preference.getString("name","")); map.put("age", String.valueOf(preference.getInt("age", 0))); account.setText(map.get("name")); passworad.setText(map.get("age")); } //儲存檔案的方法 public void save(View v) { String name=account.getText().toString(); String age=passworad.getText().toString(); prefer=new Preferences(this); prefer.save(name,Integer.valueOf(age)); 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; }}
我們看一下效果.
點擊儲存參數,出現儲存完成則說明我們已經儲存成功了,在下次登入的時候可以看到這些參數還在。因為記錄檔案是在內建空間中的,所以我們在SD卡中找不到該檔案,
如果有root許可權的手機可以下載個RE檔案管理,我們可以再/data/data/的路徑找到很多應用程式的內建檔案夾,我們可以在這些檔案夾中看到一個shared_prefs檔案夾,
裡面就有我們剛剛設定而產生的xml檔案。