how to update EditTextPreference existing summary when i click ok button
I am able to show EditTextPreference with default summary. Now i want to update it when i enter new value in Edit Text box and click ok then it should be upadate the value of summary. But i am unable to do that. My code is below please update in my code which required.
public class Prefs extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
final EditTextPreference pref = (EditTextPreference)findPreference("username");
pref.setSummary(pref.getText());
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference p开发者_运维问答reference, Object newValue) {
final EditTextPreference pref = (EditTextPreference)findPreference("username");
pref.setSummary(pref.getText());
return true;
}
});
}
}
xml file is
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="User Name"
android:key="username"
android:summary="Please provide user name" />
<EditTextPreference
android:title="Password"
android:password="true"
android:key="password"
android:summary="Please enter your password" />
</PreferenceScreen>
actually my code update previous enter value as summary text. How to show text as summary when I click ok button. Thanks
You can implement this simply by subclassing EditTextPreference
and overriding getSummary()
.
package com.example.yourapplication;
import android.content.Context;
import android.preference.EditTextPreference;
import android.text.TextUtils;
import android.util.AttributeSet;
public class FriendlyEditTextPreference extends EditTextPreference {
public FriendlyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public FriendlyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FriendlyEditTextPreference(Context context) {
super(context);
}
// According to ListPreference implementation
@Override
public CharSequence getSummary() {
String text = getText();
if (TextUtils.isEmpty(text)) {
CharSequence hint = getEditText().getHint();
if (!TextUtils.isEmpty(hint)) {
return hint;
} else {
return super.getSummary();
}
} else {
CharSequence summary = super.getSummary();
if (!TextUtils.isEmpty(summary)) {
return String.format(summary.toString(), text);
} else {
return summary;
}
}
}
}
In your xml, replace EditTextPreference
with com.example.yourapplication.FriendlyEditTextPreference
Now you can use string containing "%s" or "%1$s" as android:summary
and it will update automatically; moreover, you can set andorid:hint
so that the hint is also displayed when text is empty.
For A working ListPreference
on all platforms, see my answer of this uestion
Your preference activity doesn't appear to be implementing OnSharedPreferenceChangeListener
You may want to read over the excellent answer to the question: How do I display the current value of an Android Preference in the Preference summary?
You should use the newVal
(preference.getText()
will return the previous value as it's not yet updated)
It should look somthing like this:
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String)newValue); // TODO validate value
return true;
}
});
精彩评论