Android數據存儲之SharedPreferences

     SharedPreferences是ANDROID系統中輕量級別的鍵值存儲機制,只是存儲基本數據類型。如下是它的基本使用方法:

AndroidManifest.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.mySharedPreferences"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".mySharedPreferences"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>


</manifest> 


main.xml文件如下:

<?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"
    >
	<TextView android:text="請輸入用戶名:" 
	android:id="@+id/TextView01" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
	</TextView>
    
	<EditText android:text="@+id/EditText01" 
	android:id="@+id/EditText01" 
	android:maxLines = "1"
	android:width = "100px"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
	</EditText>
	
	<TextView android:text="請輸入密碼" 
	android:id="@+id/TextView02" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
	</TextView>
	
	<EditText android:text="@+id/EditText02" 
	android:id="@+id/EditText02" 
	android:maxLines = "1"
	android:width = "100px"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
	</EditText>
</LinearLayout>


mySharedPreferences.java文件內容如下:

package pack.mySharedPreferences;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class mySharedPreferences extends Activity {
	//定義各個元素對象
	EditText username;
	EditText password;
	//定義要保存SharedPreferences的標示
	String PREFS_NAME = "pack.mySharedPreferences.username";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //元素綁定
        username = (EditText)findViewById(R.id.EditText01);
        password = (EditText)findViewById(R.id.EditText02);
        
        //取得保存的用戶名稱,如果沒有,就設爲空
        username.setText(getUserName());
        password.setText("");
        username.setOnEditorActionListener(new OnEditorActionListener(){

			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				//保存現在的用戶名,下次使用時可讀取
				saveUserName(username.getText().toString());
				username.setText(username.getText().toString());
				return false;
			}
        });
    }
    
    private String getUserName()
    {
    	//定義一個只允許自有程序訪問的SharedPreferences
    	SharedPreferences settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
    	//獲得一個鍵值爲username的值,若Preference中不存在,則就用後面的值作爲返回值
    	String username = settings.getString("username","");
		return username;
    }
    
    private void saveUserName(String name)
    {
       	//定義一個只允許自有程序訪問的SharedPreferences
    	SharedPreferences settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
    	//生成一個保存編輯對象
    	SharedPreferences.Editor editor = settings.edit();
    	//添加要保存的鍵值和其它對應的值
    	editor.putString("username",name);
    	//提交保存
    	editor.commit();
    }
}

 

結果界面如下:

 

在DDMS裏面的DATA目錄下有如下的保存文件,其值是以XML文件格式保存,如目錄格式如下圖所示:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章