Getting Started with Android (17) Preferences 17.3 Modifying the default name of a preference file

Source: Internet
Author: User

On my device, the default preference name is Net.manoel.UsingPreferences_preferences.xml, which is prefixed with the name of the package. However, it is also useful to define a special name for a preference item. You can do this in the following way.

public class Apppreferenceactivity extends Preferenceactivity {     
    @Override public 
    void OnCreate (Bundle Savedinstancestate) {     
        super.oncreate (savedinstancestate);     
                 
        Preferencemanager prefmgr = Getpreferencemanager ();     
        Prefmgr.setsharedpreferencesname ("apppreferences");     
         
        ---Load the preferences from a XML file---     
        addpreferencesfromresource (r.xml.myapppreferences);     
    }     
}

Here, we use the Preferencemanager class to set the preference file's name as Apppreferences.xml.

At the same time, modify Usingpreferencesactivity.java this class.

The public class Usingpreferencesactivity extends activity {/** called the "when" is the "     
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     
    Setcontentview (R.layout.main); public void Onclickload (view view) {Intent i = new Intent ("Net.manoel.AppPreferenceActi     
        Vity ");     
    StartActivity (i);     
                public void Onclickdisplay (view view) {/* Sharedpreferences appprefs = Getsharedpreferences ("Net.manoel.UsingPreferences_preferences", mode_private     
        );     
         
        * * Sharedpreferences appprefs = getsharedpreferences ("apppreferences", mode_private);     
    DisplayText (appprefs.getstring ("Edittextpref", ""));     
        public void onclickmodify (view view) {/* Sharedpreferences appprefs = getsharedpreferences ("Net.manoel.UsingPreferences_prefere    
        NCEs ", mode_private);     
                 
        * * Sharedpreferences appprefs = getsharedpreferences ("apppreferences", mode_private);     
        Sharedpreferences.editor prefseditor = Appprefs.edit (); Prefseditor.putstring ("Edittextpref", (edittext) Findviewbyid (r.id.txtstring)). GetText (). ToString ()     
        );     
    Prefseditor.commit (); } private void DisplayText (String str) {Toast.maketext (Getbasecontext (), str, toast.lengt     
    H_long). Show (); }     
         
}

When we return to the application and modify the preferences, we see that the Apppreferences.xml file has been created.

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.