Let Android Preference display content changes in real-time summary

Source: Internet
Author: User

The preference available in Android can save the user's preference settings. In the Morning Star's Android staff address Book, there is a place to save the user input URL is the use of preference.

However, preference displays the value of summary by default. We want them to show the user input only.

First look at the effect:

If the user is not entered, this summary prompts the user to enter

If the user enters, this displays the user's input value

To implement the above function,

(1) Adding an XML configuration file

< edittextpreference         Android:key = "url"         android:title= "Please enter URL"            >    </ edittextpreference >

(2) Execute Onsharedpreferencechangelistener interface

 Public classSettingsfragmentextendsPreferencefragmentImplementsOnsharedpreferencechangelistener {Privateedittextpreference URL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Addpreferencesfromresource (r.xml.c21preference); URL= (edittextpreference) getpreferencescreen (). findpreference ("url"); } @Override Public voidonsharedpreferencechanged (sharedpreferences sharedpreferences, String key) {/*Get Preference*/                  if(Key.equals ("url") ) {inittextsummary (); }    }             Public voidinittextsummary () {if(Url.gettext (). Equals ("") {url.setsummary ("Please enter the interface address, e.g. http://demo.dotnetcms.org/worklog/"); }        Else{url.setsummary (Url.gettext ()); }} @Override Public voidOnresume () {Super. Onresume ();            Inittextsummary (); Getpreferencemanager (). Getsharedpreferences (). Registeronsharedpreferencechangelistener ( This); } @Override Public voidOnPause () {Getpreferencemanager (). Getsharedpreferences (). Unregisteronsharedpreferencechangelistener ( This); Super. OnPause (); }    

So you can.

(3) Get the user's configuration value

void Read (view view) {            // sharedpreferences settings = getsharedpreferences ("C21preference", Mode_ PRIVATE);          ///// Toast.maketext (Getapplicationcontext (), TXT, toast.length_short). Show ();  Sharedpreferences settings=preferencemanager.getdefaultsharedpreferences (this); String txt=settings.getstring ("url", "xxx"          

Let Android Preference summary display content changes in real time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.