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();

调用即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值