ListPreference 更改在 Kotlin 中被忽略

ListPreference changes being ignored in Kotlin

为什么我的 ListPreference 没有在偏好更改时执行所需的操作?我关注了 this video tutorial 但由于某种原因,侦听器无法正常工作。我还没有在 Kotlin 中看到任何关于如何执行此操作的工作教程。

root_preferences.xml

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

    <ListPreference
        app:defaultValue="reply"
        app:entries="@array/reply_entries"
        app:entryValues="@array/reply_values"
        app:key="list_reply"
        app:title="@string/reply_title"
        app:useSimpleSummaryProvider="true" />

</PreferenceScreen>

arrays.xml

<resources>
    <!-- Reply Preference -->
    <string-array name="reply_entries">
        <item>Reply</item>
        <item>Reply to all</item>
    </string-array>

    <string-array name="reply_values">
        <item>reply</item>
        <item>reply_all</item>
    </string-array>
</resources>

Activity

class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

       val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
       sharedPreferences.registerOnSharedPreferenceChangeListener(this)
    }

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

    override fun onDestroy() {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
        //...
        super.onDestroy()
    }

    override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
        when (key){
            "reply" -> {
                Toast.makeText(this, "Reply selected", Toast.LENGTH_SHORT).show()
            }

            "reply_to_all" -> {
                Toast.makeText(this, "Reply To All selected", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

你有:

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
    when (key){
        "reply" -> {
            Toast.makeText(this, "Reply selected", Toast.LENGTH_SHORT).show()
        }

        "reply_to_all" -> {
            Toast.makeText(this, "Reply To All selected", Toast.LENGTH_SHORT).show()
        }
    }
}

当您的 SharedPreferences 中的值发生变化时,将调用此方法。 key 是与更改的首选项值关联的键。在屏幕上显示 SharedPreferences 的情况下,该键来自 android:keyapp:key

在你的情况下,你的 app:keylist_reply:

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

    <ListPreference
        app:defaultValue="reply"
        app:entries="@array/reply_entries"
        app:entryValues="@array/reply_values"
        app:key="list_reply"
        app:title="@string/reply_title"
        app:useSimpleSummaryProvider="true" />

</PreferenceScreen>

你的 onSharedPreferenceChanged() 函数现在将完全忽略它,因为你不是在寻找 list_reply。你需要在你现有的when上添加一个分支来覆盖list_reply,比如:

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
    when (key){
        "reply" -> {
            Toast.makeText(this, "Reply selected", Toast.LENGTH_SHORT).show()
        }

        "reply_to_all" -> {
            Toast.makeText(this, "Reply To All selected", Toast.LENGTH_SHORT).show()
        }

        "list_reply" -> {
            Toast.makeText(this, "The user chose something from the ListPreference -- get the updated value from the SharedPreferences", Toast.LENGTH_SHORT).show()
        }
    }
}

而且,如果 replyreply_to_all 不是真正的键,您可以这样做:

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
    when (key) {
        "list_reply" -> {
            if (sharedPreferences.getString(key, "reply") == "reply") {
              // TODO
            }
            else {
              // TODO
            }
        }
    }
}

replyreply_to_all 不是首选项键。您偏好的键是 list_reply。你可以使用这样的方式。

       when (key){
            "list_reply" -> {
                val preference = sharedPreferences as? ListPreference
                if(preference?.value.equals("reply")){
                    //Your code
                }else if(preference?.value.equals("reply_all")){
                    //Your code
                }
            }

        }