packagecom.example.android.active;importandroid.app.Activity;importandroid.content.Context;importandroid.content.SharedPreferences;importandroid.content.SharedPreferences.Editor;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.EditText;publicclassMainActivity6extendsActivity{privateEditTexteditText;privateButtonbutton;privateSharedPreferencessharedPreferences;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.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()方法中@OverrideprotectedvoidonPause(){//TODOAuto-generatedmethodstubsuper.onPause();//读取信息数据(从xml文件中),写入editorStringmsg=editText.getText().toString();Editoreditor=sharedPreferences.edit();editor.putString("msg",msg);editor.commit();}//在onResume()方法中编写还原代码@OverrideprotectedvoidonResume(){//TODOAuto-generatedmethodstubsuper.onResume();editText.setText(sharedPreferences.getString("msg",""));/**删除信息数据(文件中的信息)Editoreditor=sharedPreferences.edit();editor.clear();editor.commit();*/}}