【问题标题】:Setting summary to describe the current value设置摘要以描述当前值
【发布时间】:2014-11-24 17:45:22
【问题描述】:

当我尝试在用户选择首选项时设置摘要时,它通常会保存。但是当我的应用重新启动时,摘要就消失了。

这是我为ListPreferenceEditTextPreference 设置摘要的代码:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){

    Preference pref = findPreference(key);

    // I feel the problem is happened here
    if (pref instanceof ListPreference) {
        ListPreference listPref = (ListPreference) pref;
        pref.setSummary(listPref.getEntry());
    }
    // Same problem here
    if (pref instanceof EditTextPreference) {
        EditTextPreference editText = (EditTextPreference) pref;
        pref.setSummary(editText.getEntry().toString());
    }
}

有什么问题吗?

【问题讨论】:

  • 你应该在 oncreate 中设置摘要
  • 参数呢?此参数 [SharedPreferences sharedPreferences, String key] 与 Preference pref = findPreference(key) 相关联。在onCreate中,这个参数(String key)不可用。
  • 如果不是,则在xml中添加android:key
  • 但我已经为 xml 中的每个首选项添加了 android:key。

标签: android sharedpreferences


【解决方案1】:
 ListPreference listPref = (ListPreference) findPreference("listkey");
 listPref.setSummary(listPref.getEntry());

 EditTextPreference editText = (EditTextPreference) findPreference("edittextkey");
 editText.setSummary(editText.getEntry().toString());

如果你有密钥,然后像上面一样设置摘要,在你的preferenceFragment或Activity中添加preferences之后的oncreate中

【讨论】:

    【解决方案2】:

    如果您只想显示当前条目,请尝试在您的 xml 中设置摘要:

    android:summary="%s"
    

    这仅适用于ListPreference(参见Doc):

    如果摘要中有一个字符串格式标记(即“%s”或“%1$s”),则当前条目值将被替换。

    【讨论】:

      【解决方案3】:

      问题可能是the listener 在启动时没有被调用(值没有改变)。但是您可以在 XML 中动态设置摘要。对于ListPreference,这是内置的,@FreshD's answer 是要走的路。要扩展到EditTextPreference,您需要创建自己的类。 For example

      package your.package;
      
      import android.content.Context;
      import android.util.AttributeSet;
      
      public class EditTextPreference extends android.preference.EditTextPreference{
              public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {
                  super(context, attrs, defStyle);
              }
      
              public EditTextPreference(Context context, AttributeSet attrs) {
                  super(context, attrs);
              }
      
              public EditTextPreference(Context context) {
                  super(context);
              }
      
              @Override
              public CharSequence getSummary() {
                  String summary = super.getSummary().toString();
                  return String.format(summary, getText());
              }
          }
      

      并在您的 xml 中使用它:

      <your.package.EditTextPreference
                      android:key="pref_alpha"
                      android:summary="Actual value: %s"
                      android:title="Title"
                      android:defaultValue="default"
                      />
      

      【讨论】:

        【解决方案4】:

        为了使摘要动态化,我费了很大力气。不完全确定这是否回答了问题,但想在某个地方发布,所以如果有人搜索,他们不必像我一样挣扎。

        这是简单的解决方案useSimpleSummaryProvider="true"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-08
          • 2011-02-25
          • 2016-01-20
          • 2012-08-20
          相关资源
          最近更新 更多