Kotlin中的ListPreference更改被忽略

问题描述 投票:0回答:2

为什么我的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>

活动

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()
            }
        }
    }
}
android kotlin android-activity sharedpreferences android-preferences
2个回答
0
投票

您有:

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

-1
投票

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

        }
© www.soinside.com 2019 - 2024. All rights reserved.