SharedPreferences,是android提供用來存儲一些簡單的配置信息的一種機制。以鍵值對的形式存儲基本的數據類型。(boolean,int,float,long,String).
應用的地方:應用程序,”設置“,”首選項“界面的保存;登陸用戶名和密碼;保存上一次用戶的修改;自定義參數的設置。
注意: 一個應用程序的配置文件只能在本應用程序中使用,只能在同一個包,不能在不同的包使用。
1根據文件名獲取指定的SharePrefrerences對象
Context.getSharedPreferences(String name,int mode)
參數說明:name:為Prefercences的文件名字。若不存在,在提交數據時自動創建Prefercences。處在同一應用程序的其他組件可以通過指定文件名字共享Prefercences。
mode:指定文件的操作模式。
Context.MODE_PRIVATE 只能由應用程序調用,默認”0“。
Context.MODE_WORLD_READABLE 允許所有應用程序有讀取文件的權限,默認”1“。
Context.MODE_PRIVATE_WRITEABLE 允許所有應用程序有寫入文件的權限,默認”2“。
SharePreferences保存數據的步驟:
1獲取對象。通過SharePreferences接口的edit獲得SharePreferences.Editor的對象。
2保存信息。通過對象SharePreferences.Editor的接口putXXX方法保存Key-value.
3提交信息。通過SharePreferences.Editor接口的commit方法保存Key-value。
查看結果:
SharedPreferences采用XML格式將數據存儲到設備中。保存在DDMS的File Explorer中的/data/data<包名>/shares_prefs下。
代碼如下:
<?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"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="用戶名:" android:id="@+id/TextView01" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android:id="@+id/nameET" android:layout_weight="7" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="密 碼:" android:id="@+id/TextView01" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android:id="@+id/passwordET" android:password="true" android:layout_weight="7" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout03" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="" android:id="@+id/TextView01" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Button android:id="@+id/loginBtn" android:layout_width="wrap_content" android:text="登陸" android:layout_height="wrap_content"></Button> <Button android:text="退出" android:id="@+id/stopBtn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout>
package com.dream; import android.app.Activity; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SharedPreferencesTestActivity extends Activity implements android.view.View.OnClickListener { /** Called when the activity is first created. */ //定義變量接收四個控件 private EditText nameET = null; private EditText passwordET = null; private Button loginBtn = null; private Button stopBtn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取四個控件的ID nameET=(EditText) findViewById(R.id.nameET); passwordET =(EditText) findViewById(R.id.passwordET); loginBtn =(Button)findViewById(R.id.loginBtn); stopBtn =(Button)findViewById(R.id.stopBtn); //在activity創建時候從Prefercens恢復登陸信息到EditText。 //獲取Activity的Preferences對象 SharedPreferences sp = getPreferences(MODE_PRIVATE); //從Preferences對象中獲取登陸信息並顯示到EditText中 nameET.setText(sp.getString("loginName", "")); passwordET.setText(sp.getString("password", "")); //設置監聽 loginBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); } @Override protected void onStop(){ //接受EidtText的值 String name = nameET.getText().toString(); String password = passwordET.getText().toString(); //定義SharedPreferences.Editor接口對象 SharedPreferences.Editor edit = getPreferences(0).edit(); //吧登陸信息保存到preferences中 edit.putString("loginName", name); edit.putString("password", password); //提交信息 edit.commit(); super.onStop(); } @Override public void onClick(View v) { // TODO Auto-generated method stub //判斷傳過來的監聽事件。 switch(v.getId()){ case R.id.loginBtn: Toast.makeText(this, "登陸成功", Toast.LENGTH_LONG).show(); break; case R.id.stopBtn:
finish(); break; default: break; } } }
遇到的問題:空指針異常。沒有獲取ID就。定義了Preferences的對象。造成了空指針異常。