我如何修改的列表preference汇总由用户(未入账价值)
How can I modify the summary of a ListPreference to the new "Entry" string selected by the user (not the entry value)
我suppouse与塞顿preferenceChangeListener(),但在
I suppouse its with setOnPreferenceChangeListener() but in
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
}
使用((名单preference)preference).getEntry()我得到的旧值(因为它没有发生变化),并newValue.toString()返回的入账价值(不进入文本显示给用户)
using ((ListPreference)preference).getEntry() I get the old value (because it isn't changed yet) and newValue.toString() returns the entry value (not the entry text displayed to the user)
我该怎么办呢?在此先感谢
How can I do it? Thanks in advance
刚刚成立汇总值%S
的XML描述。
Just set the summary value to %s
in the xml description.
修改:我已经在多个设备上测试它,它不工作真的。这是奇怪,因为根据文档必须工作:List$p$pference.getSummary().
EDIT: I've tested it on several devices and it doesn't work really. That's strange because according to docs it must work: ListPreference.getSummary().
但它可能自己来实现这个功能。的执行需要从目录preference继承
类:
But it's possible to implement this functionality by oneself. The implementation requires to inherit from the ListPreference
class:
public class MyListPreference extends ListPreference {
public MyListPreference(final Context context) {
this(context, null);
}
public MyListPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
@Override
public CharSequence getSummary() {
final CharSequence entry = getEntry();
final CharSequence summary = super.getSummary();
if (summary == null || entry == null) {
return null;
} else {
return String.format(summary.toString(), entry);
}
}
@Override
public void setValue(final String value) {
super.setValue(value);
notifyChanged();
}
}
正如你可以看到 MYLIST preference
具有可包含格式标记自己的汇总字段。而当preference值发生变化, preference.notifyChanged()
方法被调用,它会导致 MYLIST preference.getSummary ()
从 preference.onBindView()
。
As you can see MyListPreference
has its own summary field which can contain formatting markers. And when a preference's value changes, Preference.notifyChanged()
method is called and it causes MyListPreference.getSummary()
method to be called from Preference.onBindView()
.
PS:这种方法还没有经过充分的测试,因此它可能包含错误
P.S.: This approach hasn't been tested sufficiently so it may contain errors.