preferenceActivity更新摘要 [英] PreferenceActivity update summary

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

问题描述

我有一个 preferenceActivity preferenceScreen S,就像一个2级的树

I have a PreferenceActivity with a 2 level tree of PreferenceScreens, something like:

<PreferenceScreen>
  <PreferenceScreen android:key="A">
     <ListPreference/>
     <EditTextPreference/>
  </PreferenceScreen>
  <PreferenceScreen android:key="B">
     <ListPreference/>
     <EditTextPreference/>
  </PreferenceScreen>
  ...
</PreferenceScreen>

每个下级preference屏幕,例如,A和B,在收集两个相关的数据片段。我想摘要中那些父件是两个子preferences的当前值的组合

Each of the lower level preference screens, e.g., A and B, is collecting two related pieces of data. I want the summary for those parent items to be a combination of the current values of the two sub preferences.

我试图对叶preferences将在preferenceChangeListener的,并从那里更新汇总,但它似乎并没有采取。在preferences都是编程方式创建活动中,所以我做这样的事情在的onCreate:

I tried adding onPreferenceChangeListener's on the leaf preferences and updating the summary from there, but it doesn't seem to take. The preferences are all created programmatically within the activity, so I'm doing something like this in onCreate:

   leafListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
       @Override
       public boolean onPreferenceChange(Preference preference, Object newValue) {
              // do some work
              prefScreenA.setSummary( /* get new summary based on newValue */);
              return true;
       }
  });

然后我试图找到一个位置,我被告知我已经回到了顶级preferences屏幕从子页面,这样我可以在这一点上更新。不过,我很困惑,如何在较低的水平屏幕显示。它看起来像他们实际上是对话,而不是完整的活动开展以来的onPause / onResume它们之间移动时,不叫。有一些方法的地方,我很想念当返回到顶级页面时,将调用?

I then tried to find a location where I can be informed that I have returned to the top level preferences screen from a subpage so that I can update at that point. However, I am confused as to how the lower level screens are being displayed. It looks like they are actually dialogs, not full Activities since onPause/onResume is not called when moving between them. Is there some method somewhere that I'm missing that will be called when returning to the top level page?

我也尝试添加共享preferenceChangeListener,如<一个href="http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-$p$pference-in-the-$p$pference-sum">here,但似乎永远不会被调用。

I also tried adding a sharedPreferenceChangeListener, as described here, but that never seems be called.

任何人有任何的提示对我缺少的是什么在这里,或者更简单的方法,我失踪?

Anyone have any hints on what I'm missing here, or any easier approach I'm missing?

推荐答案

我有同样的问题,似乎调用的 Activity.onContentChanged()的汇总变更后纠正了我的问题。

I had the same problem and it seems that calling Activity.onContentChanged() after the summary is changed corrected my problem.

@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
  // Update summary value
  EditTextPreference pref = (EditTextPreference)findPreference(key);
  pref.setSummary(pref.getText());
  this.onContentChanged();
}

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

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