简单存储指的是Android系统提供的轻量级的数据保存方式SharedPreferences,将数据以最简单的方式进行永久的存储,SharedPreferences屏蔽了对底层的文件操作,为程序员提供简单的程序接口,实现基于关键字的数据保存。
在使用SharedPreferences前,先定义SharedPreferences的访问模式。(SharedPreferences支持3种访问模式:私有(MODE_PRIVATE)、全局读(MODE_WORLD_READABLE)、全局写(MODE_WORLD_WRITEABLE))。
public static int MODE = MODE_PRIVATE;
除了定义SharedPreferences的访问模式,还需要定义SharedPreferences的名称
public static final String PREFERENCE_NAME = "SaveSetting";
使用SharedPreferences时需要将访问模式和SharedPreferences名称作为参数传递到getSharedPreferences()函数,则可获取SharedPreferences实例。
SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
在获取SharedPreferences实例后,可以通过SharedPreferences.Editor类对SharedPreferences进行修改,然后调用commit()函数保存修改内容。
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Name", nameText.getText().toString());
editor.putInt("Age", Integer.parseInt(ageText.getText().toString()));
editor.putFloat("Height",
Float.parseFloat(heightText.getText().toString()));
editor.commit();
如果需要从保存的SharedPreferences中读取数据,同样调用getSharedPreferences()函数。
SharedPreferences sharedPreferences = getSharedPreferences(
PREFERENCE_NAME, MODE);
String name = sharedPreferences.getString("Name", "Tom");
int age = sharedPreferences.getInt("Age", 20);
float height = sharedPreferences.getFloat("Height", 1.78f);
源码:
MainActivity.java
public class MainActivity extends Activity {
private EditText nameText, ageText, heightText;
//定义SharedPreferences的名称
public static final String PREFERENCE_NAME = "SaveSetting";
//定义SharedPreferences的访问模式:全局读+全局写
public static int MODE = Context.MODE_WORLD_READABLE
+ Context.MODE_WORLD_WRITEABLE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText) findViewById(R.id.editText1);
ageText = (EditText) findViewById(R.id.editText2);
heightText = (EditText) findViewById(R.id.editText3);
}
@Override
protected void onStart() {
loadSharedPreferences();
super.onStart();
}
@Override
protected void onStop() {
saveSharedPreferences();
super.onStop();
}
private void loadSharedPreferences() {
SharedPreferences sharedPreferences = getSharedPreferences(
PREFERENCE_NAME, MODE);
String name = sharedPreferences.getString("Name", "Tom");
int age = sharedPreferences.getInt("Age", 20);
float height = sharedPreferences.getFloat("Height", 1.78f);
//读取数据
nameText.setText(name);
ageText.setText(String.valueOf(age));
heightText.setText(String.valueOf(height));
}
private void saveSharedPreferences() {
SharedPreferences sharedPreferences = getSharedPreferences(
PREFERENCE_NAME, MODE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Name", nameText.getText().toString());
editor.putInt("Age", Integer.parseInt(ageText.getText().toString()));
editor.putFloat("Height",
Float.parseFloat(heightText.getText().toString()));
editor.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="39dp"
android:text="姓名:" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="43dp"
android:text="年龄:" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/editText2"
android:ems="10" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_marginTop="42dp"
android:text="身高:" />
</RelativeLayout>
SaveSetting.xml保存在/data/data/<package name>/shared_prefs目录下。
效果图:
修改信息,退出再次进入即可看到保存的信息。。