android2.3 api demo 学习系列(8)--App/Activity/Preference State

android保存数据有很多种方式,其中最简单的就是使用SharedPreferences。Shared Preferences存储的是key/value键值对。支持的数据类型为:boolean、long、float、int、string、stringset。SharedPreferences在保存UI的state上最为常用方便。(application之间不能使用SharedPreferences,仅限在同一个application内activity之间共享一些数据)

 

1、创建布局

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="4dip"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip" 
        android:text="@string/app_activity_persistent_state_msg"/>

    <TextView
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/app_activity_persistent_state_save"/>

    <EditText android:id="@+id/app_activity_persistent_state_saved_edit"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/green"
        android:text="@string/app_activity_persistent_state_save"
        android:freezesText="true">
        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingTop="8dip"
        android:paddingBottom="4dip"
        android:text="@string/app_activity_persistent_state_cancel"/>

    <EditText 
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/red"
        android:text="@string/app_activity_persistent_state_cancel">
    </EditText>

</LinearLayout>

2、在activity的onPause方法内保存数据

@Override
protected void onPause() {
	super.onPause();
	SharedPreferences.Editor editor = getPreferences(this.MODE_PRIVATE).edit();
	editor.putString("text", mEditText.getText().toString());
        editor.putInt("selection-start", mEditText.getSelectionStart());
        editor.putInt("selection-end", mEditText.getSelectionEnd());
        editor.commit();
}

  3、在activity的onResume方法内还原数据

 

@Override
protected void onResume() {
	super.onResume();
	SharedPreferences prefs = getPreferences(0); 
        String restoredText = prefs.getString("text", null);
        if (restoredText != null) {
            mEditText.setText(restoredText, TextView.BufferType.EDITABLE);

            int selectionStart = prefs.getInt("selection-start", -1);
            int selectionEnd = prefs.getInt("selection-end", -1);
            if (selectionStart != -1 && selectionEnd != -1) {
            	mEditText.setSelection(selectionStart, selectionEnd);
            }
        }
}

 效果图:


android2.3 api demo 学习系列(8)--App/Activity/Preference State_第1张图片

 

-----------------------------------------------------------------------------------------------------------------------

接下来我们来看看sdk中关于SharePreferences的相关介绍

SharedPreferences 类提供了基本的保存key-value对来原始数据. 你可以使用SharedPreferences保存此类数据: booleans, floats, ints, longs, and strings. 数据将会被保存到用户的session中(即使应用程序被终止)。

 

在应用程序中如果想使用 SharedPreferences 可以使用下面的两个方法:

使用SharePreferences写入数据的步骤:
  1. Call edit() to get a SharedPreferences.Editor.
  2. Add values with methods such as putBoolean() and putString().
  3. Commit the new values with commit()
同样对应的存在get方法来读取数据
示例:
public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}
 

你可能感兴趣的:(android,api,demo)