根据另一个首选项的更改刷新首选项的 EditText

发布于 2024-10-20 09:02:07 字数 963 浏览 7 评论 0原文

我想根据另一个首选项的更改来更改 PreferenceActivity 中每个 Preference显示的值。

我有一个在 Preferences 中存储指标(浮点)值的应用程序。我还有一个名为 metricPreference,用户可以在公制或英制单位的使用之间切换。

我只存储公制值,但是当用户切换到英制时,应用程序会显示英制值。

如果用户仅切换指标首选项并离开 PreferenceActivity,一切都会正常工作。当他返回时,值会以正确的方式显示。例如,如果他查看 userHeight,显示屏仍然会显示指标值。

在我的 PreferenceActivity 中,我想在 metric 首选项更改时刷新显示的值(我确保在存储这些值时它们会正确转换回来):

this.metric.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final Boolean metric = Boolean.parseBoolean(newValue.toString());
        changeSummaries(metric);
        refreshDisplay(metric); 
        return true;
    }
});

什么我必须在 refreshDisplay(...) 方法中实现吗?它还有助于了解 PreferenceActivity 生命周期的哪个点提交更改的值?

I want to change the values displayed in each Preference in my PreferenceActivity based on the change in another preference.

I have an app that stores metric (float) values in Preferences. I also have a Preference called metric where the user can switch between usage of metric or imperial units.

I only store metric values, but when the user switches to imperial the app displays the imperial values.

Everything works fine if the user only switches the metric preference and leaves the PreferenceActivity. When he goes back the values are displayed the correct way. If he goes to look at userHeight for example the display still shows the metric value though.

In my PreferenceActivity I want to refresh the displayed values when the metric Preference changes (I am making sure that when these values should be stored they will be converted back correctly):

this.metric.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final Boolean metric = Boolean.parseBoolean(newValue.toString());
        changeSummaries(metric);
        refreshDisplay(metric); 
        return true;
    }
});

What do I have to implement in the refreshDisplay(...) method? It would also help to know at what point of the lifecycle of the PreferenceActivity are the changed values committed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-10-27 09:02:07

我找到了。至少对于我的具体问题,我找到了一个简单的解决方案。

我已经扩展了 EditTextPreference 以便能够存储除 String 之外的其他内容

在打开对话框之前,调用 onBindDialogView 方法,此时另一个更改Preference 已提交(我一开始以为可能不是 - 快速测试证明我错了)。只需重写 onBindDialogView 就可以将显示的值更改为我想要显示的值:

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);
    getEditText().setText(getPersistedString(""));
}

@Override
protected String getPersistedString(String defaultReturnValue) {
    final Float storedValue = getPersistedFloat(-1);
    Float returnValue;
    if (MyPreferences.isMetric(getContext())) {
        returnValue = storedValue;
    } else {
        returnValue = this.unit.convertToImperial(storedValue);
    }
    return String.valueOf(returnValue);
}

I found it. At least for my specific problem I found a simple solution.

I had already extended EditTextPreference to be able to store something other than String

Before opening the dialog the onBindDialogView method is called at that point the other changed Preference is committed (I thought at first it might not be - a quick test proved me wrong). Simply overriding onBindDialogView allows me to change the displayed value to the one I want to display:

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);
    getEditText().setText(getPersistedString(""));
}

@Override
protected String getPersistedString(String defaultReturnValue) {
    final Float storedValue = getPersistedFloat(-1);
    Float returnValue;
    if (MyPreferences.isMetric(getContext())) {
        returnValue = storedValue;
    } else {
        returnValue = this.unit.convertToImperial(storedValue);
    }
    return String.valueOf(returnValue);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文