Editor edit = shared.edit();//获取编辑器
edit.putString("numberpwd",content);
edit.commit();//提交修改
例:
public static boolean write(Context context) {
// 获取SharedPreferences对象
/**
* param1: 创建的xml的名称
* param2: 该xml的访问的权限(Context去获取 mode)
*/
SharedPreferences shared = context.getSharedPreferences(Constant.PATH,
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
// 获取到他的编辑器
Editor editor = shared.edit();
editor.putString("name", "zs");
editor.putInt("age", 18);
editor.putFloat("weight", 80.6f);
// 提交
editor.commit();
return true;
}
public static boolean read(Context context) {
SharedPreferences shared = context.getSharedPreferences(Constant.PATH,
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
String name = shared.getString("pwd", null);
int age = shared.getInt("age", -1);
float weight = shared.getFloat("weight", 0.0f);
Log.d("FileIOUtilsShared", "name=" + name + " age=" + age + " weight="
+ weight);
return true;
}
本文介绍了如何使用Android的SharedPreferences对象进行数据的存储与读取操作,包括获取SharedPreferences对象、设置数据类型(字符串、整数、浮点数)、提交修改及读取已存储的数据。通过实例演示了如何实现简单的数据持久化。
200

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



