【问题标题】:Preference summary with current value throug custom EditTextPreference: how to get current value?通过自定义 EditTextPreference 具有当前值的偏好摘要:如何获取当前值?
【发布时间】:2012-03-21 22:58:06
【问题描述】:

how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-summary 的启发,我创建了自己的 EditTextPreference,它在 PreferenceScreen 中显示其当前值。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="preferences" xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory 
        android:title="First Category"
        android:key="first_category">

        <de.k3b.widgets.EditTextPreferenceWithSummary
            android:key="test"
            android:title="Test Message" 
            android:dialogTitle="Test Message"
            android:dialogMessage="Provide a message"   
            android:defaultValue="Default welcome message" />

    </PreferenceCategory>
</PreferenceScreen>

实现看起来像这样

package de.k3b.widgets;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.*;
import android.util.AttributeSet;
import android.util.Log;

public class EditTextPreferenceWithSummary extends EditTextPreference {
    private final static String TAG = EditTextPreferenceWithSummary.class.getName();

    public EditTextPreferenceWithSummary(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EditTextPreferenceWithSummary(Context context) {
        super(context);
        init();
    }

    private void init() {
        Log.e(TAG, "init");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());     
        String currentText = test prefs.getString("minTrashholdInSecs", this.getText());

// where do i get the current value of the underlaying EditTextPreference ??
//      vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        this.setSummary(this.getText());
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                Log.w(TAG, "display score changed to "+newValue);
                preference.setSummary(newValue.toString()); // getSummary());
                return true;
            }
        });
    }
}

当 PreferenceScreen 首次显示时,没有显示当前值。

我的问题:我从哪里获得 EditTextPreference 代表的当前值? getText() 没有得到我预期的值。 更改首选项值后,此值将按预期显示在摘要字段中。

我正在使用安卓 2.2

【问题讨论】:

    标签: android widget sharedpreferences


    【解决方案1】:

    尝试将以下代码添加到您的 EditTextPreferenceWithSummary 类中:

    @Override
    protected View onCreateView(ViewGroup parent) {
        this.setSummary(this.getText());
        return super.onCreateView(parent);
    }
    

    对我来说,它有效。 我认为问题在于您试图从 UI 线程中更改 UI 状态(实际上是在构造函数中)。

    【讨论】:

      【解决方案2】:

      这是一个更完整的示例,包括值何时更新,而无需安装首选项侦听器。

      import android.content.Context;
      import android.preference.EditTextPreference;
      import android.util.AttributeSet;
      import android.view.View;
      import android.view.ViewGroup;
      
      public class EditTextPreferenceWithValueSummary extends EditTextPreference{
      
        public EditTextPreferenceWithValueSummary(Context context) {
          super(context);
        }
      
        public EditTextPreferenceWithValueSummary(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      
        @Override
        protected View onCreateView(ViewGroup parent) {
            this.setSummary(this.getText());
            return super.onCreateView(parent);
        }
      
        @Override
        protected void onDialogClosed(boolean positiveResult) {
            super.onDialogClosed(positiveResult);
      
            if (positiveResult) {
              this.setSummary(getText());
            }
        }
      }
      

      在你的xml/settings.xml:

      <your.package.views.EditTextPreferenceWithValueSummary
              android:key="some_preference_key"
              android:title="Some Title" />
      

      【讨论】:

      • +1 这个解决方案确实比我的更优雅。 @Oleg Flores 得到了接受的答案,因为这解决了我在 2012 年的问题。
      猜你喜欢
      • 1970-01-01
      • 2020-12-16
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2019-03-20
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多