package com.example.android029;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity6 extends Activity {
private EditText et;
private Button btn;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
et=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button_send);
//获取共享属性操作的工具类(文件名,操作模式)
sp=getSharedPreferences("data",0);
}
//在onPause方法中编写保存数据
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
String msg=et.getText().toString();
Editor editor=sp.edit();
editor.putString("msg", msg);
editor.commit();
}
//在onResume方法编写放回数据
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//还原数据
et.setText(sp.getString("msg", ""));
Editor editor=sp.edit();
editor.clear();
editor.commit();
}
}
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity6 extends Activity {
private EditText et;
private Button btn;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
et=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button_send);
//获取共享属性操作的工具类(文件名,操作模式)
sp=getSharedPreferences("data",0);
}
//在onPause方法中编写保存数据
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
String msg=et.getText().toString();
Editor editor=sp.edit();
editor.putString("msg", msg);
editor.commit();
}
//在onResume方法编写放回数据
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//还原数据
et.setText(sp.getString("msg", ""));
Editor editor=sp.edit();
editor.clear();
editor.commit();
}
}