1.声明一个共享参数(存储数据方便的api)
private SharedPreferences sp;
//2.通过上下文得到一个共享参数的实例对象
//第一个参数:文件名称 第二个:模式,一般指定为私有
sp = this.getSharedPreferences("config",MODE_PRIVATE);
//3.
Editor editor = sp.edit();
editor.putString("account",account);
editor.putString("password",password);
//一定不要忘记提交数据
editor.commit();//提交数据(类似关闭流)
--------------------------------------
//读取sp
String account = sp.getString("account","");
String password = sp.getString("password","");SharePreference
共享参数存储详解
本文介绍了如何使用Android中的共享参数(SharedPreferences)进行数据存储。详细步骤包括:声明共享参数实例、编辑并提交数据、读取存储的数据等。适用于需要在应用程序中进行简单数据持久化的开发者。

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



