onSharedPreferenceChanged 问题中的 setSummary(txt)
我在 XML 文件中定义了一个 PreferenceScreen,如下所示:
<PreferenceCategory android:title="Choose Days">
<PreferenceScreen android:title="Days of Week" android:key="daysOfWeek">
<CheckBoxPreference android:title="Mondays" android:key="chkMonday"></CheckBoxPreference>
<CheckBoxPreference android:title="Tuesdays" android:key="chkTuesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Wednesdays" android:key="chkWednesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Thursdays" android:key="chkThursday"></CheckBoxPreference>
<CheckBoxPreference android:title="Fridays" android:key="chkFriday"></CheckBoxPreference>
<CheckBoxPreference android:title="Saturdays" android:key="chkSaturday"></CheckBoxPreference>
<CheckBoxPreference android:title="Sundays" android:key="chkSunday"></CheckBoxPreference>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings">
<CheckBoxPreference android:title="Enable" android:defaultValue="true" android:key="enable"></CheckBoxPreference>
</PreferenceCategory>
当我单击 PreferenceScreen:daysOfWeek 时,会出现复选框,当我选中或取消选中某个框时,会触发 onSharedPreferenceChanged。
这是因为:
公共类 Muteny 扩展 PreferenceActivity 实现 OnSharedPreferenceChangeListener {
并在 onResume 中注册它,并在我的 .java 文件中的 onPause 中注销它。
简化的 onSharedPreferenceChanged 看起来像这样:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
PreferenceScreen daysOfWeek = ((PreferenceScreen)findPreference("daysOfWeek"));
daysOfWeek.setSummary("text for summary");
Toast.makeText(this.getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
}
问题是“daysOfWeek”的摘要永远不会更新。如果我随后将 CheckBoxPreference 切换为“启用”(此时不执行任何操作,但会触发更改),则摘要会按照我在切换“日期框”时希望的方式进行更新。
但是,Toast 中的消息显示...
如何在检查“日期框”时更新我的 PreferenceScreen 的摘要?
I have a PreferenceScreen defined in an XML-file like this:
<PreferenceCategory android:title="Choose Days">
<PreferenceScreen android:title="Days of Week" android:key="daysOfWeek">
<CheckBoxPreference android:title="Mondays" android:key="chkMonday"></CheckBoxPreference>
<CheckBoxPreference android:title="Tuesdays" android:key="chkTuesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Wednesdays" android:key="chkWednesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Thursdays" android:key="chkThursday"></CheckBoxPreference>
<CheckBoxPreference android:title="Fridays" android:key="chkFriday"></CheckBoxPreference>
<CheckBoxPreference android:title="Saturdays" android:key="chkSaturday"></CheckBoxPreference>
<CheckBoxPreference android:title="Sundays" android:key="chkSunday"></CheckBoxPreference>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings">
<CheckBoxPreference android:title="Enable" android:defaultValue="true" android:key="enable"></CheckBoxPreference>
</PreferenceCategory>
When I click on the PreferenceScreen:daysOfWeek the checkboxes appear, og when I check or uncheck a box, the onSharedPreferenceChanged is triggered.
This is because of this:
public class Muteny extends PreferenceActivity implements OnSharedPreferenceChangeListener {
and register it in onResume and unregister it in onPause in my .java file.
A simplified onSharedPreferenceChanged looks like this:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
PreferenceScreen daysOfWeek = ((PreferenceScreen)findPreference("daysOfWeek"));
daysOfWeek.setSummary("text for summary");
Toast.makeText(this.getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
}
The problem is that the summary of "daysOfWeek" is never updated. If I then switch the CheckBoxPreference "enable" (which does nothing at the moment, but trigger the change), the summary is updated as I wanted it to do when switching the "day-boxes".
The message in Toast IS displayed though...
How do I update the summary of my PreferenceScreen when checking the "day boxes"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题经常出现。
您可以查看以下几个其他答案以寻找可能的解决方案。但我从来没有找到一个让我 100% 满意的解决方案。
PreferenceScreen android:summary 更新!
从(子)PreferenceScreen 返回时更新 PreferenceActivity 中的现有首选项
在我见过的所有“解决方案”中,似乎效果最好的一个是在 setSummary(...) 调用之后调用 onContentChanged() 。关于这样做是否安全存在一些争论。
This issue comes up frequently.
Here are a couple of other answers you can look at for possible solutions. I've never settled on a solution that makes me 100% happy though.
PreferenceScreen android:summary update !
Update existing Preference-item in a PreferenceActivity upon returning from a (sub)PreferenceScreen
Of all of the "solutions" I've seen the one that seems to work best is to call onContentChanged() after your setSummary(...) call. There is some debate as to whether this is safe to do.