2010年10月19日星期二

使用SharedPreferences

SharedPreferences類提供一個保存和檢索持久的鍵值對原始數據類型的框架.

可以使用SharedPreferences保存任何原始數據:布爾型(booleans),浮點(floats),整數(ints),長整數(ongs)和字符串(strings). 這些數據將保持不變(即使應用程序被終止).

使用SharedPreferences

要獲得 SharedPreferences對象, 可以使用兩種方法之一:

- getSharedPreferences() : 如果你需要使用多個Preferences文件, 使用第一個參數指定Preferences文件的名稱.
- getPreferences() : 如果只需要一個Preferences文件.

要寫入:
1。呼叫edit()來獲得SharedPreferences.Editor.
2。呼叫如putBoolean()putString()添加值.
3。呼叫commit()提交新的值.

讀取, 使用使用SharedPreferences的方法,如getBoolean()getString().

範例:

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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/preferencestring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/savepreference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Preference"
/>
<Button
android:id="@+id/restorepreference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Restore Preference"
/>
<Button
android:id="@+id/exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>


主代碼, AndroidPreferences.java
package com.AndroidPreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidPreferences extends Activity {

EditText myText;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myText = (EditText)findViewById(R.id.preferencestring);
Button buttonSave = (Button)findViewById(R.id.savepreference);
Button buttonRestore = (Button)findViewById(R.id.restorepreference);
Button buttonExit = (Button)findViewById(R.id.exit);

buttonSave.setOnClickListener(buttonSaveOnClickListener);
buttonRestore.setOnClickListener(buttonRestoreOnClickListener);
buttonExit.setOnClickListener(buttonExitOnClickListener);

SharedPreferences myPreferences = getPreferences(0);
String myPreferencesString = myPreferences.getString("key", "default value");

myText.setText(myPreferencesString);

}

private Button.OnClickListener buttonSaveOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key", myText.getText().toString());
editor.commit();

}};

private Button.OnClickListener buttonRestoreOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences myPreferences = getPreferences(0);
String myPreferencesString = myPreferences.getString("key", "default value");

myText.setText(myPreferencesString);
}};

private Button.OnClickListener buttonExitOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}};

}




沒有留言:

發佈留言