PreferenceFragmentCompat中如何处理ListPreference中的每一项

How to handle each item in ListPreference in PreferenceFragmentCompat

class SettingsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey)

    }
}

root_preferences

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory app:title="@string/language_setting_header">
    <ListPreference
        app:defaultValue="en"
        app:entries="@array/language_entries"
        app:entryValues="@array/language_values"
        app:key="@string/key_lang"
        app:title="@string/language_setting_title"
        app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

为了

 onPreferenceTreeClick

仅在显示对话框时触发,但不在 ListPreference 中的每个项目上触发

而不是听列表中的每一项

prefChange 时监听。还有 registListener onResume 和 onPause

Kotlin 中 PreferenceFragmentCompat 的解决方案

class SettingsFragment : PreferenceFragmentCompat() {
    private val  prefListener = SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
        when (key) {
            "lang" -> {
                activity?.finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(
            prefListener
        )
    }

    override fun onPause() {
        preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(
            prefListener
        )
        super.onPause()
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey)
        preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(
            prefListener
        )
    }

}

https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat

我就是这样做的。

class SettingsFragment : PreferenceFragmentCompat() {
    var prefListener = SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
        when (key) {
            "lang" -> {
                activity?.finish()
            }
        }
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey)
        preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(
            prefListener
        )
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:orderingFromXml="true">

   <ListPreference
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:defaultValue="0"
       android:entries="@array/array_update"
       android:entryValues="@array/array_update_hours"
       android:key="update"
       android:summary="@string/pref_summary_update"
       android:title="@string/pref_title__update" />