ListPreference摘要未格式化 [英] ListPreference summary not formatting

查看:92
本文介绍了ListPreference摘要未格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListPreference,我想在摘要中显示当前条目.根据 docs的ListPreference.getSummary(),我应该能够通过在摘要字符串中包含%s来做到这一点.不幸的是,该活动仅在摘要中显示%s.

I have a ListPreference and I want to show the current entry in the summary. According to the docs for ListPreference.getSummary(), I'm supposed to be able to do this by including %s in the summary string. Unfortunately, the activity just displays the %s in the summary.

XML非常标准:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference
        android:key="displayMode"
        android:summary="@string/display_mode_summary"
        android:title="@string/display_mode"
        android:defaultValue="BOTH"
        android:entries="@array/displayModes"
        android:entryValues="@array/displayModeValues"
        />
</PreferenceScreen>

字符串display_mode_summary的值仅为%s. (值"BOTH"存在于displayModeValues数组中.)如果我将ListPreference子类化为这样:

The value of the string display_mode_summary is just %s. (The value "BOTH" is present in the displayModeValues array.) If I subclass ListPreference like this:

public final class DisplayModePreference extends ListPreference {
    // ...

    @Override
    public CharSequence getSummary() {
        return String.format(super.getSummary().toString(), getEntry());
    }
}

然后,当首选项活动开始时,会将当前值正确地插入到摘要中.但是,当我单击首选项并从对话框中选择其他值时,对话框关闭时,摘要仍显示现在的值.我需要关闭首选项"活动并重新启动以查看更改.

then when the preferences activity starts, the current value is correctly interpolated into the summary. But when I click on the preference and select a different value from the dialog, when the dialog closes the summary still shows the now-old value. I need to close the preferences activity and restart it to see the change.

我已经在不同API级别的多个仿真器中进行了尝试.我需要什么才能使显示的摘要始终反映当前值?

I've tried this in several emulators at different API levels. What do I need to so that the displayed summary always reflects the current value?

推荐答案

您可以按如下所示覆盖"onDialogClosed":

You could override "onDialogClosed" as below:

@override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
        setSummary(getEntry());
    }
}

这会将您的首选项摘要设置为所选条目的文本.

This will set the summary of your preference to the text of the selected entry.

这篇关于ListPreference摘要未格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆