自定义SharedPreferences的封装
来源:互联网 发布:android图片压缩算法 编辑:程序博客网 时间:2024/04/28 19:07
SpUtil类是对android.content.SharedPreferences的封装,简化了对其的使用。代码如下:
public class SpUtil { private static final String NAME="EY"; //保存文件的名称 private static SpUtil instance; public static SpUtil getInstance(){ if(instance==null){ instance=new SpUtil(); } return instance; } private SharedPreferences getSharePerference(Context context){ return context.getSharedPreferences(NAME, Context.MODE_PRIVATE); } public String getString(Context context,String key){ return SpUtil.getInstance().getSharePerference(context).getString(key, null); } public void setString(Context context,String key,String value){ Editor editor=SpUtil.getInstance().getSharePerference(context).edit(); editor.putString(key, value); editor.commit(); } public boolean getBoolean(Context context,String key){ return SpUtil.getInstance().getSharePerference(context).getBoolean(key, false); } public void setBoolean(Context context,String key,boolean value){ Editor editor=SpUtil.getInstance().getSharePerference(context).edit(); editor.putBoolean(key, value); editor.commit(); } public int getInt(Context context,String key){ return SpUtil.getInstance().getSharePerference(context).getInt(key, 0); } public void setInt(Context context,String key,int value){ Editor editor=SpUtil.getInstance().getSharePerference(context).edit(); editor.putInt(key, value); editor.commit(); } public Set<String> getStringSet(Context context,String key){ return SpUtil.getInstance().getSharePerference(context).getStringSet(key, null); } public void setStringSet(Context context,String key,Set<String> value){ Editor editor=SpUtil.getInstance().getSharePerference(context).edit(); editor.putStringSet(key, value); editor.commit(); }}
注:使用时直接调用即可,例如String类型,调用代码如下:
SpUtil.getInstance().setString(this, "hello", "hello world");String str=SpUtil.getInstance().getString(this, "hello");
0 0
- 自定义SharedPreferences的封装
- 自定义简单的封装SharedPreferences缓存类
- 关于SharedPreferences的封装
- sharedpreferences的封装
- SharedPreferences的简单封装
- SharedPreferences的简单封装
- SharedPreferences的封装
- Android SharedPreferences的简单封装
- 封装SharedPreferences的工具类
- Android 对SharedPreferences的封装
- Kotlin封装SharedPreferences的操作
- 工具类的封装--SharedPreferences封装
- SharedPreferences封装
- SharedPreferences封装
- 封装 SharedPreferences
- SharedPreferences封装
- SharedPreferences封装
- SharedPreferences封装
- 手把手教你把 md 文件(markdown)转换为 docx 或者 pdf
- js实现数字千分位逗号分开格式化(银行金额效果)
- swift通过字符串获取类型实例化对象问题
- Android全局异常捕获,不退出应用,让应用正常运行下去!
- oKHttp网络请求
- 自定义SharedPreferences的封装
- 深度学习物体检测——常用资源简述
- android设置小技巧
- 使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误
- DataTable学习笔记---排序细则、列隐藏[3]
- React-native 常见错误:Could not get BatchedBridge, make sure your bundle is packaged correctly
- [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Python列表解析学习
- jquery插件地址