Android的名单preferences:有汇总为所选值?所选、名单、Android、preferences

2023-09-05 05:37:20 作者:檐上三寸雪

我有使用List preferences我的XML文件中所定义的preference活动。我将如何设置列表活动的总结,所选择的价值呢?

I have a Preference activity which uses a List Preferences as defined by my XML file. How would I set the summary of the list activity to the value that is selected?

感谢您!

推荐答案

要做到这一点最简单的方法就是让机器人为你做。假设你要总结匹配选择的值,你可以简单地set的目录preference摘要%的 使用XML或在 setSummary 的Java方法。例如:

The simplest way to do this is just to have Android do it for you. Assuming you want the summary to match the selected value, you can simply set the summary of the ListPreference to "%s" using either XML or the setSummary method in Java. For example:

<ListPreference
    android:key="pref_list"
    android:title="A list of preferences"
    android:summary="%s"
    android:entries="@array/pref_list_entries"
    android:entryValues="@array/pref_list_entries_values"
    android:defaultValue="0" />

Android将取代%S 用的preference当前字符串值,所显示的目录preference 的选择器。

Android will replace %s with the current string value of the preference, as displayed by the ListPreference's picker.

我花的据的太多时间摆弄共享preferences 之前,我发现了这个监听器。作为奖励,该列表的总结将正确设置,当你进入活动。

I spent far too much time mucking about with SharedPreferences listeners before I discovered this. As a bonus, the list's summary will be set correctly when you enter the activity.