恋你自学魔鬼式课程之SharedPreferences(保存参数)
1.SharedPreferences 用于保存参数的接口(以xml格式保存)
2.定义:SharedPreferences sp=null;
3.初始化:sp=getSharedPreferences(name,mode);//存储地址默认到data/data/包名/
4.得到SharedPreferences的编辑器
Editor editor=sp.edut();
5.存储不同类型的数据(基本数据类型)
/**
*编辑需要存储的数据
*@key xml节点名
*@value 数据内容
*/
editor.putString(key,value);
String pwd="mima2013";
editor.putString("mima",pwd);
editor.putBoolean("bl",true);
// 清空编辑器里面的内容
//editor.clear();
// 提交以上编辑的内容
editor.commit();
6.获取到SharedPreferences生成的xml文件
/**
*获取关联的xml文件中的内容
*@key 获取xml的节点
*@defValue 找不到该节点,返回默认值
*/
boolean is=sp.getBoolean(key,defValue);
boolean is=sp.getBoolean("bl",false);
String name=sp.getString("pwd","");