为什么我的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()
}
}
}
}
您有:
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:key
或app:key
。
在您的情况下,您的app:key
是list_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()
}
}
}
而且,如果reply
和reply_to_all
不是真正的键,则可以执行以下操作:
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
"list_reply" -> {
if (sharedPreferences.getString(key, "reply") == "reply") {
// TODO
}
else {
// TODO
}
}
}
}
reply
和reply_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
}
}
}