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); } } }
效果图:
-----------------------------------------------------------------------------------------------------------------------
接下来我们来看看sdk中关于SharePreferences的相关介绍
SharedPreferences
类提供了基本的保存key-value对来原始数据. 你可以使用SharedPreferences
保存此类数据: booleans, floats, ints, longs, and strings. 数据将会被保存到用户的session中(即使应用程序被终止)。
在应用程序中如果想使用 SharedPreferences
可以使用下面的两个方法:
getSharedPreferences(String name, int mode)
- 如果你想用到多个preferences,请使用该方法并指定名称.getPreferences(int mode)
- 如果只需要一个preferences,应该使用该方法.edit()
to get a SharedPreferences.Editor
.putBoolean()
and putString()
.commit()
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(); } }