android sharedpreferences 工具类,GitHub - open-android/SharedPreferencesUtils: Android SharedPreference...

欢迎关注微信公众号、长期为您推荐优秀博文、开源项目、视频

微信公众号:Android干货程序员

687474703a2f2f6f69356e716e3663652e626b742e636c6f7564646e2e636f6d2f69746865696d612f626f6f737465722f636f64652f7172636f64652e706e67

使用步骤

1. 在project的build.gradle添加如下代码(如下图)

allprojects {

repositories {

...

maven { url "https://jitpack.io" }

}

}

2. 在Module的build.gradle添加依赖

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、付费专栏及课程。

余额充值