java文件
package edu.hrbeu.SimplePreferenceDemo;
import edu.hrbue.SimplePreferenceDemo.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class SimplePreferenceDemoActivity extends Activity {
private EditText nameText;
private EditText ageText;
private EditText heightText;
public static final String PREFERENCE_NAME = "SaveSetting";
public static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText)findViewById(R.id.name);
ageText = (EditText)findViewById(R.id.age);
heightText = (EditText)findViewById(R.id.height);
}
@Override
public void onStart(){
super.onStart();
loadSharedPreferences();
}
@Override
public void onStop(){
super.onStop();
saveSharedPreferences();
}
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.81f);
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();
}
}
布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText android:id="@+id/name"
android:text=""
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip" >
</EditText>
<TextView android:id="@+id/name_label"
android:text="姓名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/name"
android:layout_alignBaseline="@+id/name">
</TextView>
<EditText android:id="@+id/age"
android:text=""
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_below="@id/name"
android:numeric="integer">
</EditText>
<TextView android:id="@+id/age_label"
android:text="年龄:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/age"
android:layout_alignBaseline="@+id/age" >
</TextView>
<EditText android:id="@+id/height"
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_below="@id/age"
android:numeric="decimal">
</EditText>
<TextView android:id="@+id/height_label"
android:text="身高:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/height"
android:layout_alignBaseline="@+id/height">
</TextView>
</RelativeLayout>