SharedPreferences封装类

最近一直在读马伟奇老师的简书,给人以不一样的感觉,接下来的时间会做做笔记,毕竟好东西变成自己的才有用

原文地址SharedPreferencesUtils

 依赖

dependencies {
compile 'com.github.open-android:SharedPreferencesUtils:0.1.0
}
保存数据到sharedpreferences
SharedPreferencesUtils.init(context).putBoolean("boolean", true);
//可以使用字符串资源作为保存的键
SharedPreferencesUtils.init(context).putBoolean(R.string.key_bool, true);
//可以自定义sharedpreferences的文件名
SharedPreferencesUtils.init(context,"Custom").putBoolean("boolean", true);
一句代码保存多条数据(链式调用)
SharedPreferencesUtils.init(context)
.putBoolean(R.string.key_bool, true)
.putInt(R.string.key_int, 1)
.putString(R.string.key_string, "string")
.putLong(R.string.key_long, 1000000000)
.putFloat(R.string.key_float, 1.1f)
.put("put", 100)
.putStringSet(R.string.key_set, strings);
读取数据
boolean booleanData = SharedPreferencesUtils.init(context).getBoolean("bool");

boolean booleanData = SharedPreferencesUtils.init(context).getBoolean(R.string.key_bool,defValue);
移除某个键对应的数据
SharedPreferencesUtils.init(context).remove(bool);

SharedPreferencesUtils.init(context).remove(R.string.key_bool);
清除所有数据
SharedPreferencesUtils.init(context).clear();

调用即可

posted @   嘉禾世兴  阅读(213)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示