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.xml
string1=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();
}
}
用SharedPreferences存储数据,一个最简单的例子
最新推荐文章于 2019-05-23 21:03:41 发布
本文介绍如何在Android应用中使用SharedPreferences进行数据的持久化存储,并通过代码示例展示了如何将数据保存到SharedPreferences文件中,以及如何从该文件中读取数据。此功能对于在应用的不同页面间传递状态信息或保存用户偏好设置非常有用。
325

被折叠的 条评论
为什么被折叠?



