你怎么刷新preferenceActivity以显示变化的设置? [英] How do you refresh PreferenceActivity to show changes in the settings?

查看:257
本文介绍了你怎么刷新preferenceActivity以显示变化的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下code,你能告诉我如何刷新preferenceActivity窗口来显示变化立即设置?例如:用户点击主磬切换复选框以真(打勾的),我希望用户能够立即看到其他设置,如ChimeOn15Past复选框也是真(打勾)

 共享preferences.Editor prefEditor = clockSettings.edit(); //允许的设置来改变。

如果(booleanMasterChimeToggle ==真){
    prefEditor.putBoolean(ChimeOnTheHour,真正的);
    prefEditor.putBoolean(ChimeOn15Past,真正的);
    prefEditor.putBoolean(ChimeOn30Past,真正的);
    prefEditor.putBoolean(ChimeOn45Past,真正的);

    strNotifyMessage =全钟声已经设置完毕。

} 其他 {
    prefEditor.putBoolean(ChimeOnTheHour,假);
    prefEditor.putBoolean(ChimeOn15Past,假);
    prefEditor.putBoolean(ChimeOn30Past,假);
    prefEditor.putBoolean(ChimeOn45Past,假);

    strNotifyMessage =全钟声已经被禁用。
}
 

解决方案

尼古拉的接受的答案是正确的。我只想补充一些code在这里更清楚地说明他的观点。

 私人复选框preference mOn15Past;
私人复选框preference mOn30Past;
私人复选框preference mOn45Past;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //从XML资源负载的preferences
    加preferencesFromResource(R.xml preferences。);

    mOn15Past =(复选框preference)找到preference(ChimeOn15Past);
    mOn30Past =(复选框preference)找到preference(ChimeOn30Past);
    mOn45Past =(复选框preference)找到preference(ChimeOn45Past);

    最后preference chimeMaster =找到preference(ChimeMaster);
    chimeMaster.setOn preferenceChangeListener(新在preferenceChangeListener(){
        @覆盖
        在preferenceChange公共布尔(preference preference,对象的newval){
            最终布尔值=(布尔)的newval;
            mOn15Past.setChecked(值);
            mOn30Past.setChecked(值);
            mOn45Past.setChecked(值);
            返回true;
        }

    });
}
 

总之, preferenceActivity 不是用来一旦开始从持久存储刷新其价值。而不是使用共享preferences.Editor 修改和提交的其他变化就像你在code一样,最好是进行更改到本地 preferenceManager 对象将由 preferenceActivity 在正常的生命周期被提交。

Based on the following code, can you tell me how to refresh the PreferenceActivity window to show changes in the settings immediately? For example: the user taps the master chime toggle checkbox to true (ticked), I would like the user to immediately see the other settings such as the ChimeOn15Past checkbox also be be true (ticked)

SharedPreferences.Editor prefEditor = clockSettings.edit(); // Allow the settings to be changed.

if (booleanMasterChimeToggle == true) {
    prefEditor.putBoolean("ChimeOnTheHour", true);
    prefEditor.putBoolean("ChimeOn15Past", true);
    prefEditor.putBoolean("ChimeOn30Past", true);
    prefEditor.putBoolean("ChimeOn45Past", true);

    strNotifyMessage = "Full chiming has now been set.";

} else {
    prefEditor.putBoolean("ChimeOnTheHour", false);
    prefEditor.putBoolean("ChimeOn15Past", false);
    prefEditor.putBoolean("ChimeOn30Past", false);
    prefEditor.putBoolean("ChimeOn45Past", false);

    strNotifyMessage = "Full chiming has now been disabled.";
}

解决方案

Nikolay's accepted answer is correct. I just want to add some code here to illustrate his point more clearly.

private CheckBoxPreference mOn15Past;
private CheckBoxPreference mOn30Past;
private CheckBoxPreference mOn45Past;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    mOn15Past = (CheckBoxPreference) findPreference("ChimeOn15Past");
    mOn30Past = (CheckBoxPreference) findPreference("ChimeOn30Past");
    mOn45Past = (CheckBoxPreference) findPreference("ChimeOn45Past");

    final Preference chimeMaster = findPreference("ChimeMaster");
    chimeMaster.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newVal) {
            final boolean value = (Boolean) newVal;
            mOn15Past.setChecked(value);
            mOn30Past.setChecked(value);
            mOn45Past.setChecked(value);
            return true;
        }

    });
}

In short, PreferenceActivity is not designed to refresh its values from the persistent storage once it is started. Instead of using SharedPreferences.Editor to modify and commit additional changes like you did in your code, it is better to make the changes into the local PreferenceManager object which will be committed by the PreferenceActivity in its normal lifecycle.

这篇关于你怎么刷新preferenceActivity以显示变化的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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