import android.content.Context
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class MyPreference1<T>(privateval context: Context,val name: String,privateval default: T): ReadWriteProperty<Any?, T>{privateval prefs: SharedPreferences by lazy {
context.getSharedPreferences("default", Context.MODE_PRIVATE)}overridefungetValue(thisRef: Any?, property: KProperty<*>): T {returngetPreference(name, default)}overridefunsetValue(thisRef: Any?, property: KProperty<*>, value: T){putPreference(name, value)}@Suppress("UNCHECKED_CAST")privatefun<T>getPreference(name: String, default: T): T =with(prefs){val res: Any =when(default){is Long ->getLong(name, default)is String ->getString(name, default)is Int ->getInt(name, default)is Boolean ->getBoolean(name, default)is Float ->getFloat(name, default)else->throwIllegalArgumentException("This type can not be saved into Preferences")}
res as T
}privatefun<U>putPreference(name: String, value: U)=with(prefs.edit()){when(value){is Long ->putLong(name, value)is String ->putString(name, value)is Int ->putInt(name, value)is Boolean ->putBoolean(name, value)is Float ->putFloat(name, value)else->throwIllegalArgumentException("This type can be saved into Preferences")}.apply()}}
方式二:
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import kotlin.reflect.KProperty
class MyPreference2<T>(val name: String,privateval default: T){privateval prefs: SharedPreferences by lazy {
App.instance.applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE)}operatorfungetValue(thisRef: Any?, property: KProperty<*>): T {returngetSharePreferences(name, default)}operatorfunsetValue(thisRef: Any?, property: KProperty<*>, value: T){putSharePreferences(name, value)}@Suppress("UNCHECKED_CAST")privatefungetSharePreferences(name: String, default: T): T =with(prefs){val res: Any =when(default){is Long ->getLong(name, default)is String ->getString(name, default)is Int ->getInt(name, default)is Boolean ->getBoolean(name, default)is Float ->getFloat(name, default)else->throwIllegalArgumentException("This type of data cannot be saved!")}return res as T
}@SuppressLint("CommitPrefEdits")privatefunputSharePreferences(name: String, value: T)=with(prefs.edit()){when(value){is Long ->putLong(name, value)is String ->putString(name, value)is Int ->putInt(name, value)is Boolean ->putBoolean(name, value)is Float ->putFloat(name, value)else->throwIllegalArgumentException("This type of data cannot be saved!")}.apply()}}
class App :Application(){companionobject{// 伴生对象lateinitvar instance: App
privateset}overridefunonCreate(){super.onCreate()
instance =this}}
测试
class MainActivity :AppCompatActivity(){var a byMyPreference1(this@MainActivity,"a",1)var b byMyPreference2("b",100)@SuppressLint("SetTextI18n")overridefunonCreate(savedInstanceState: Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)
tv_result.text =""+ a
}@SuppressLint("SetTextI18n")@Suppress("UNUSED_EXPRESSION")funonclick(view: View){when(view.id){
R.id.btn1 -> a++
R.id.btn2 -> tv_result.text =""+ a
R.id.btn3 -> b++
R.id.btn4 -> tv_result.text =""+ b
else->"default"}}}