1.编辑strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Data_SharedPreferencesActivity!</string>
<string name="app_name">Data_SharedPreferences</string>
<string name="tvName">姓名</string>
<string name="tvAge">年龄</string>
<string name="btSet">设置</string>
<string name="btRead">读取</string>
<string name="saveSuccess">保存成功</string>
<string name="saveFiled">保存失败</string>
</resources>
2.编辑main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
//姓名
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvName"
android:layout_width="25pt"
android:layout_height="wrap_content"
android:text="@string/tvName"/>
<EditText
android:id="@+id/etName"
android:layout_width="300pt"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toRightOf="@id/tvName"/>
</RelativeLayout>
//年龄
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvAge"
android:layout_width="25pt"
android:layout_height="wrap_content"
android:text="@string/tvAge"/>
<EditText
android:id="@+id/etAge"
android:layout_width="300pt"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toRightOf="@id/tvAge"/>
//etAge位于tvAge的右边
</RelativeLayout>
//按钮
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btSet"
android:layout_width="50pt"
android:layout_height="wrap_content"
android:text="@string/btSet"/>
<Button
android:id="@+id/btRead"
android:layout_width="50pt"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btSet"
android:text="@string/btRead"/>
</RelativeLayout>
</LinearLayout>
3.编辑Data_SharedPreferencesActivity.java
package wei.cao.data;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Data_SharedPreferencesActivity extends Activity {
private EditText etName;
private EditText etAge;
private Button btRead;
private Button btSet;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取按钮和编辑框
etName=(EditText)this.findViewById(R.id.etName);
etAge=(EditText)this.findViewById(R.id.etAge);
btRead=(Button)this.findViewById(R.id.btRead);
btSet=(Button)this.findViewById(R.id.btSet);
//添加事件
btSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//获取输入的姓名和年龄
String name=etName.getText().toString();
String age=etAge.getText().toString();
//取得活动的Preferences对象
SharedPreferences sp=getPreferences(Activity.MODE_PRIVATE);
//取得编辑对象
Editor editor=sp.edit();
//添加值
editor.putString("name", name);
editor.putInt("age", Integer.parseInt(age));
//保存数据
if(editor.commit())
{
Toast.makeText(Data_SharedPreferencesActivity.this, R.string.saveSuccess, 1).show();
}
else
{
Toast.makeText(Data_SharedPreferencesActivity.this, R.string.saveFiled, 1).show();
}
}
});
btRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//创建SharedPreferences对象
SharedPreferences sp=getPreferences(MODE_PRIVATE);
//获取数据(第二个参数代表,如果没有找到name,那么name的值就为defaultName,)
//就是把name改为na,那么name的值就是defaultName
String name=sp.getString("name", "defaultName");
String age=sp.getInt("age", 0)+"";
//显示数据
String result="name:"+name+"===age:"+age;
Toast.makeText(Data_SharedPreferencesActivity.this, result, 1).show();
}
});
}
}
4.执行结果:
5.打开DDMS的File Explore 在data/data/wei.cao.data会产生一个文件,
导出后的结果为:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="name">qwe</string><int name="age" value="12" /></map>