【发布时间】: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