package com.example.android.active; import android.app.Activity; import android.content.Context; 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 editText; private Button button; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main6); button=(Button) findViewById(R.id.test); editText=(EditText) findViewById(R.id.EditText_main6_1); //获取共享属性操作的工具(文件名,操作模式) //MODE_PRIVATE/0,私有模式,只能本应用访问 //MODE_WORLD_READABLE(读)和MODE_WORLD_WRITEABLE(写)不建议使用(其他因公可以直接读取) sharedPreferences=this.getSharedPreferences("data", 0); } //在onPause()方法中编写保存数据的代码 //还原在onResume()方法中 @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); //读取信息数据(从xml文件中),写入editor String msg=editText.getText().toString(); Editor editor=sharedPreferences.edit(); editor.putString("msg", msg); editor.commit(); } //在onResume()方法中编写还原代码 @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); editText.setText(sharedPreferences.getString("msg", "")); /* * 删除信息数据(文件中的信息) Editor editor=sharedPreferences.edit(); editor.clear(); editor.commit(); */ } }
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1881434
原文地址:http://matengbing.blog.51cto.com/11395502/1881434