使用示例
保存一个基本数据获取获取单个基本数据
sp().putString("key","hhhhh")
保存多个基本数据
sp().edit {
putInt(SpKey.TEST,2)
putString(SpKey.HELLO,"hello")
}
保存对象和获取对象
sp().putAny(SpKey.TEST,TestBean(true,"alice"))
//获取保存对象
val test = sp().getAny<TestBean>(SpKey.TEST)
保存集合和获取集合
val list = listOf(TestBean(true,"alice"),TestBean(true,"bob"))
sp().putAny(SpKey.TEST,list)*/
//获取保存的list
/* val testList = sp().getList<TestBean>(SpKey.TEST)
Log.e("MMM", "testSp: $testList" )*
删除数据
//清除某个数据
sp().clear(SpKey.HELLO)
//清除当前name下的所有数据
sp().clear()
封装类
```kotlin fun Any.sp(name: String = LUtilsConfig.SP_DEFAULT_NAME) = LUtilsConfig.getContext().getSharedPreferences(name, Context.MODE_PRIVATE)
/**
批处理 */ fun SharedPreferences.edit(action: SharedPreferences.Editor.() -> Unit) { edit().apply { action() }.apply() }
/**
对象操作 */ fun SharedPreferences.putAny(key: String, obj: Any?) { putString(key, obj?.toJson() ?: “”) }
/*
获取任意对象
*/
fun
inline fun
/*
获取list
*/
fun
inline fun
/**
put系列 */ fun SharedPreferences.putString(key: String, value: String?) { edit { putString(key, value ?: “”) } }
fun SharedPreferences.putInt(key: String, value: Int) { edit { putInt(key, value) } }
fun SharedPreferences.putBoolean(key: String, value: Boolean) { edit { putBoolean(key, value) } }
fun SharedPreferences.putFloat(key: String, value: Float) { edit { putFloat(key, value) } }
fun SharedPreferences.putLong(key: String, value: Long) { edit { putLong(key, value) } }
fun SharedPreferences.putStringSet(key: String, value: Set
fun SharedPreferences.clear(key: String) { edit { remove(key) } }
fun SharedPreferences.clear() { edit { clear() } } ```