使用SharedPreferences在程序的数据空间中生成xml文档来保存数据
package com.hu.data;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ShDemoActivity extends Activity {
private EditText etName,etAge,etScore;
private Button btWrite,btRead;
private SharedPreferences sharedPrefrences;
private Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etName = (EditText) findViewById(R.id.editTextName);//得到控件
etAge = (EditText) findViewById(R.id.editTextAge);
etScore = (EditText) findViewById(R.id.editTextScore);
btWrite = (Button) findViewById(R.id.buttonWrite);
btRead = (Button) findViewById(R.id.buttonRead);
sharedPrefrences = this.getSharedPreferences("user", MODE_WORLD_READABLE);//得到SharedPreferences,会生成user.xml
editor = sharedPrefrences.edit();
btWrite.setOnClickListener(new OnClickListener() {//写入按钮事件
public void onClick(View arg0) {
String name = etName.getText().toString();
int age = Integer.parseInt(etAge.getText().toString());
float score = Float.parseFloat(etScore.getText().toString());//获取用户输入数据
editor.putString("name", name);
editor.putInt("age", age);
editor.putFloat("score", score);//将数据写入xml
editor.commit();//提交
}
});
btRead.setOnClickListener(new OnClickListener() {//读出按钮事件
public void onClick(View v) {
String name = sharedPrefrences.getString("name", null);
int age = sharedPrefrences.getInt("age", 0);
float score = sharedPrefrences.getFloat("score", 60.0f);//将数据读出
etName.setText(name);
etAge.setText(Integer.toString(age));
etScore.setText(Float.toString(score));//显示数据
}
});
}
}
布局文件为:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分数:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入" />
<Button
android:id="@+id/buttonRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读出" />
</LinearLayout>
</LinearLayout>
SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。