美文网首页
Kotlin 使用Perference

Kotlin 使用Perference

作者: wanTag | 来源:发表于2018-08-27 15:35 被阅读5次
    实例:
    • 模板类
    • 委托属性/委托代理
    • lazy修饰符
    • with函数
    class Perference<T>(val context: Context, val name: String, val default: T)
        : ReadWriteProperty<Any?, T> {
    
        /***
         * 通过属性代理初始化共享参数对象
         * lazy:第一次使用时执行初始化
         */
        val prefs: SharedPreferences by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) }
    
        /***
         * 接管属性值的获取行为
         * *:表示一个不确定的类型
         */
        override fun getValue(thisRef: Any?, property: KProperty<*>): T {
            return findPreference(name, default)
        }
    
    
        /***
         * 接管属性值的修改行为
         */
        override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            putPreference(name, value)
        }
    
        //利用with函数定义临时的命名空间
        private fun <T> findPreference(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 -> throw IllegalArgumentException("This type can be saved into Preferences")
            }
            return res as T
        }
    
        private fun <T> putPreference(name: String, value: T) = with(prefs.edit()) {
            //putInt、putString等方法返回Editor对象
            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 -> throw IllegalArgumentException("This type can be saved into Preferences")
            }.apply() //commit方法和apply方法都表示提交修改
        }
    
    }
    
    调用
    class PerferenceActivity : AppCompatActivity() {
    
        /***
         * by委托属性/委托代理
         */
        private var name: String by Perference(this, "name", "")
        private var age: String by Perference(this, "age", "")
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_perference)
    
            btn_save.setOnClickListener { v ->
                when {
                    et_name.text.isEmpty() -> toast("请先填写name")
                    et_age.text.isEmpty() -> toast("请先填写age")
                    else -> {
                        name = et_name.text.toString()
                        age = et_age.text.toString()
                    }
                }
            }
    
            btn_read.setOnClickListener { v ->
                tv_show.text = name + "\n" + age
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Kotlin 使用Perference

          本文链接:https://www.haomeiwen.com/subject/iuabiftx.html