gpt4 book ai didi

android - 为什么每次从 Kotlin 的 ListPreference 中选择一个项目后,val 变量总是会改变?

转载 作者:行者123 更新时间:2023-11-29 18:37:03 24 4
gpt4 key购买 nike

ListPreference 绑定(bind)键 "@string/WindowScaleName",val oldValue 来自同一个键 "@string/WindowScaleName"

变量oldValue是val,我以为它会保持不变,但实际上,它总是在我从ListPreference中选择一个项目后发生变化,我没有为什么。你可以如下图。

顺便说一句,val other 保持不变!

XML

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="@string/PreferenceSummary"
android:title="@string/PreferenceTitle" >

<ListPreference
android:key="@string/WindowScaleName"
android:entries="@array/WindowsScale"
android:entryValues="@array/WindowsScale_value"
android:defaultValue="@string/WindowScaleDefaultValue"
android:title="@string/PreferenceWindowScaleTitle"
/>

</PreferenceScreen>

代码A

class UIPreference : AppCompatPreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_preference)
fragmentManager.beginTransaction().replace(R.id.content, MyPreferenceFragment()).commit()

}

class MyPreferenceFragment : PreferenceFragment() {
private lateinit var mContext: Context

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.mypreference)
mContext=this.activity
val mActivity=this.activity

bindListPreferenceSummaryToValue( findPreference(mContext.getString(R.string.WindowScaleName)) )

val windowScaleListPreferenec = preferenceManager.findPreference(getString(R.string.WindowScaleName)) as ListPreference
val oldValue: String by PreferenceTool(mContext, getString(R.string.WindowScaleName), getString(R.string.WindowScaleDefaultValue) )
val other=oldValue

logError("Me :$oldValue other:$other")

windowScaleListPreferenec.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->

var isRestartApp:Boolean by PreferenceTool(mContext, getString(R.string.IsRestartAppName), false)
isRestartApp = !(oldValue==newValue.toString())

logError("old: $oldValue other:$other new: $newValue istrue: $isRestartApp")
true
}
}
}
}

代码B

fun bindListPreferenceSummaryToValue(preference: Preference) {
preference.onPreferenceChangeListener = sBindListPreferenceSummaryToValueListener

sBindListPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.context)
.getString(preference.key, ""))
}

private val sBindListPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->
val stringValue = value.toString()
if (preference is ListPreference) {
val listPreference = preference
val index = listPreference.findIndexOfValue(stringValue)

preference.setSummary(
if (index >= 0)
listPreference.entries[index]
else
preference.summary)

}
true
}

代码 C

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

图片 enter image description here

最佳答案

oldValue 是一个 local delegated property ,不仅仅是一个简单的只读变量。

这意味着每次访问它时都会调用一个委托(delegate)。没有什么能阻止它每次返回不同的值。但是您不能自己设置它的值(因为它是 val)。

关于android - 为什么每次从 Kotlin 的 ListPreference 中选择一个项目后,val 变量总是会改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com