ListPreference
связать ключ "@string/WindowScaleName"
, и значение val oldValue
от того же ключа "@string/WindowScaleName"
Переменная oldValue
равна val, я думаю, что она не изменится, нона самом деле, он всегда меняется после выбора элемента из ListPreference
, я не знаю почему.Вы можете на следующем изображении.
Кстати, значение 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()
}
}
Изображение