如何为Kotlin中的某个属性编写带有延迟的委托代码?

时间:2018-12-01 03:39:29

标签: android kotlin

我希望为委托人isBackgroundWindow设置属性PreferenceTool(在代码B中定义)。

但是代码1 private var isBackgroundWindow: Boolean by PreferenceTool(this@UIHome, getString(R.string.IsBackgroundName) , false)导致空错误。您可以在https://hastebin.com/ohasirorut.cs

看到错误日志

代码2 无法编译,代码3 也无法正常工作。

如何为isBackgroundWindow属性定义委托?

还有,代码0获取信息'lateinit'修饰符在委派属性上是不允许的

代码0

 private  lateinit  var isBackgroundWindow: Boolean by PreferenceTool(this, getString(R.string.IsBackgroundName) , false)

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_home)
    isBackgroundWindow=true
 }

代码1

class UIHome : AppCompatActivity() {
    private var isBackgroundWindow: Boolean by PreferenceTool(this@UIHome, getString(R.string.IsBackgroundName) , false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_home)
        isBackgroundWindow=true
    }
}

代码2

 class UIHome : AppCompatActivity() {
       private var isBackgroundWindow: Boolean by lazy {
            PreferenceTool<Boolean>(this@UIHome, getString(R.string.IsBackgroundName), false)
       }

       override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.layout_home)
            isBackgroundWindow=true
        }
    }

代码3

 private lateinit  var isBackgroundWindow: Boolean

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_home)  

        isBackgroundWindow= PreferenceTool(this, getString(R.string.IsBackgroundName) , false)
   }

代码B

 class PreferenceTool<T>(private val context: Context, private val name: String,  private val default: T) {

        private val prefs: SharedPreferences by lazy {      
            context.defaultSharedPreferences     
        }

        operator fun getValue(thisRef: Any?, property: KProperty<*>): T 
                          = findPreference(name, default)

        operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            putPreference(name, value)
        }

        @Suppress("UNCHECKED_CAST")
        private fun 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")
            }

            res as T
        }

        @SuppressLint("CommitPrefEdits")
        private fun putPreference(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 -> throw IllegalArgumentException("This type can't be saved into Preferences")
            }.apply()
        }
    }

0 个答案:

没有答案