2017-09-30 28 views
0

以下代碼是關於Kotlin的示例項目,我可以使用Code 1來獲取共享首選項的值,但是我可以設置共享首選項的值?如何在Kotlin中設置值首選項?

我無法在示例項目中找到這些代碼,您能告訴我我該怎麼辦?謝謝!

代碼1

class SettingsActivity : AppCompatActivity() { 

    companion object { 
     val ZIP_CODE = "zipCode" 
     val DEFAULT_ZIP = 94043L 
    } 

    var zipCode: Long by DelegatesExt.preference(this, ZIP_CODE, DEFAULT_ZIP) 
} 

代碼2

object DelegatesExt { 
    fun <T> notNullSingleValue() = NotNullSingleValueVar<T>() 
    fun <T> preference(context: Context, name: String, default: T) = Preference(context, name, default) 
} 

class NotNullSingleValueVar<T> { 

    private var value: T? = null 

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T { 
     return value ?: throw IllegalStateException("${property.name} not initialized") 
    } 

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { 
     this.value = if (this.value == null) value 
     else throw IllegalStateException("${property.name} already initialized") 
    } 
} 

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

    val prefs: SharedPreferences by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) } 

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T { 
     return 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 
    } 

    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() 
    } 
} 

和更多

如果函數putPreference是公共的,我可以使用設置的共享偏好的值下面的代碼,但它的醜陋

class SettingsActivity : AppCompatActivity() { 

    companion object { 
     val ZIP_CODE = "zipCode" 
     val DEFAULT_ZIP = 94043L 
    } 

    DelegatesExt.Preference(this, ZIP_CODE, DEFAULT_ZIP).putPreference(ZIP_CODE,"99999L"); 
} 

回答

3

operator fun setValue是什麼:你只寫

activity.zipCode = 1L 

(其中activitySettingsActivity)或

zipCode = 1L 

(內SettingsActivity或類擴展它),它我們將致電,致電putPreference("zipCode", 1L)。有關更多信息,請參見https://kotlinlang.org/docs/reference/delegated-properties.html

+0

謝謝!你能寫一個完整的代碼來設置共享首選項的值嗎?我不明白爲什麼'activity.zipCode =「MyNewZIP」'將調用setValue(activity,「zipCode」,...)' – HelloCW

+0

因爲這就是委派屬性如何定義的工作。你有沒有讀過鏈接? –

+0

您不能使用'「MyNewZIP」',因爲類型是「Long」(這對於郵政編碼來說並不合適)。 –

相關問題