根据另一个首选项的更改刷新首选项的 EditText
我想根据另一个首选项的更改来更改 PreferenceActivity
中每个 Preference
中显示的值。
我有一个在 Preferences
中存储指标(浮点)值的应用程序。我还有一个名为 metric
的 Preference
,用户可以在公制或英制单位的使用之间切换。
我只存储公制值,但是当用户切换到英制时,应用程序会显示英制值。
如果用户仅切换指标首选项并离开 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了。至少对于我的具体问题,我找到了一个简单的解决方案。
我已经扩展了
EditTextPreference
以便能够存储除String
之外的其他内容在打开对话框之前,调用
onBindDialogView
方法,此时另一个更改Preference
已提交(我一开始以为可能不是 - 快速测试证明我错了)。只需重写onBindDialogView
就可以将显示的值更改为我想要显示的值: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 thanString
Before opening the dialog the
onBindDialogView
method is called at that point the other changedPreference
is committed (I thought at first it might not be - a quick test proved me wrong). Simply overridingonBindDialogView
allows me to change the displayed value to the one I want to display: