是wanandroid老哥项目上的 private const val SP_WANANDROID = "spwanandroid" @JvmOverloads fun <T> getSpValue( filename: String = SP_WANANDROID, context : Context, key : String, defaultVal : T) : T{ val sp = context.getSharedPreferences(filename,Context.MODE_PRIVATE) return when (defaultVal) { is Boolean -> sp.getBoolean(key, defaultVal) as T is String -> sp.getString(key, defaultVal) as T is Int -> sp.getInt(key, defaultVal) as T is Long -> sp.getLong(key, defaultVal) as T is Float -> sp.getFloat(key, defaultVal) as T is Set<*> -> sp.getStringSet(key, defaultVal as Set<String>) as T else -> throw IllegalArgumentException("Unrecognized default value $defaultVal") } } @JvmOverloads fun <T> putSpValue( filename: String = SP_WANANDROID, context: Context, key: String, value: T ){ val editor = context.ge
android kotlin对sp进行封装
最新推荐文章于 2022-04-03 20:39:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)