简体   繁体   中英

Is possible to change the summary of EditTextPreference dynamically in Android?

I set up a preferenceScreen to edit the settings in my application. I would like to insert an EditTextPreference that contains a Title like "set your name" and a summary containing the name entered.

Is that possible? thank you in advance!

Sure, here is a short example:

EditTextPreference etp = (EditTextPreference) findPreference("the_pref_key");
etp.setSummary("New summary");

This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment , since findPreference() is a method of these classes. You most likely do that already.

To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM