用SharedPreferences存储数据,一个最简单的例子

来源:互联网 发布:网络流行语 四个字 编辑:程序博客网 时间:2024/06/07 06:57
package com.learn.sd2;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.widget.EditText;public class mainm extends Activity {private EditText t1;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);t1=(EditText) findViewById(R.id.EditText01);String string1;SharedPreferences store=getSharedPreferences(“storexml”,0);//建立storexml.xmlstring1=store.getString(“row1″, “”);//从storexml.xml中读取上次进度,存到string1中t1.setText(string1);//把上次进度显示在屏幕}protected void onStop() {super.onStop();SharedPreferences store=getSharedPreferences(“storexml”,0);SharedPreferences.Editor editor=store.edit();editor.putString(“row1″, t1.getText().toString());editor.commit();}}

原创粉丝点击