我正在尝试在设置页面中显示项目摘要。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="@bool/show_button_default"
android:key="@string/textview_visibility_key"
android:summaryOff="@string/hidden"
android:summaryOn="@string/shown"
android:title="Show TextView"/>
<EditTextPreference
android:defaultValue="@string/default_textview_text"
android:key="@string/textview_text_key"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Change TextView's Text" />
<ListPreference
android:defaultValue="@string/pref_bckgrnd_colour_black_value"
android:entryValues="@array/background_colour_values"
android:key="@string/textview_backgroundcolor_key"
android:entries="@array/background_colour_labels"
android:title="TextView Background Color" />
<ListPreference
android:defaultValue="@string/pref_txt_colour_white_value"
android:entries="@array/text_colour_labels"
android:entryValues="@array/text_colour_values"
android:key="@string/textview_textcolor_key"
android:title="TextView Text Color"/>
</PreferenceScreen>
SettingsFragment.kt
import android.os.Bundle
import android.preference.CheckBoxPreference
import android.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import android.util.Log
class SettingsFragement : PreferenceFragmentCompat(){
val TAG = "SettingsFragement"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_pref)
val sharedPref = preferenceScreen.sharedPreferences
val prefScreen = preferenceScreen
val count = preferenceScreen.preferenceCount
for (index in 0..(count-1)) {
val preference = prefScreen.getPreference(index)
val prefKey = preference.key
if (preference !is CheckBoxPreference) {
try {
val value = sharedPref.getString(prefKey, "")
setPreferenceSummary(preference, value)
} catch (e : Exception){
Log.e(TAG, "Caught CheckBoxPreference Exception, "+e.message)
}
}
}
}
fun setPreferenceSummary(preference: Preference, value: String) {
Log.d(TAG, "Preference $preference is ListPreference = ${preference is ListPreference}")
if (preference is ListPreference) {
val prefIndex = preference.findIndexOfValue(value)
Log.d(TAG, "prefIndex is $prefIndex")
if (prefIndex >= 0)
preference.setSummary(preference.entries[prefIndex])
}
}
}
但仍然不会显示摘要
这是我的日志输出:
E / SettingsFragement:引发CheckBoxPreference异常,无法将java.lang.Boolean强制转换为java.lang.String
D / SettingsFragement:首选项更改TextView的文本为ListPreference = false
D / SettingsFragement:首选项TextView背景颜色为ListPreference = false
首选项TextView文本颜色为ListPreference = false
因此,我决定有意将偏好(在setPreferenceSummary中)强制转换为ListPreference,然后我捕获了异常。
fun setPreferenceSummary(preference: Preference, value: String) {
Log.d(TAG, "Preference $preference is ListPreference = ${preference is ListPreference}")
if (preference is ListPreference) {
val prefIndex = preference.findIndexOfValue(value)
Log.d(TAG, "prefIndex is $prefIndex")
if (prefIndex >= 0)
preference.setSummary(preference.entries[prefIndex])
}
try {
val prefIndex = (preference as ListPreference).findIndexOfValue(value)
} catch (e : Exception){
Log.e(TAG, e.message)
}
}
例外:
E / SettingsFragement:android.support.v7.preference.ListPreference无法转换为android.preference.ListPreference
我没有在设置布局中使用android.support.v7.preference.ListPreference,也没有导入它。
请帮助我,错误是从哪里来的,为什么强制转换不起作用?