Getting Started with Android (17) Preferences 17.2 encoding implementation get and modify values in preset information

Source: Internet
Author: User

As you've seen in the previous section, Preferenceactivity This class allows users to create and modify preference information at run time. If you want to use these preference information, you need to use the Sharedpreferences class.

1. Continue to use the works in the previous section to add some code to the Usingpreferences.java.

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);     
    DisplayText (appprefs.getstring ("Edittextpref", ""));      
                public void onclickmodify (view view) {Sharedpreferences Appprefs = Getsharedpreferences ("Net.manoel.UsingPreferences_preferences", 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 (); }     
         
}

2. Press F11 to debug on the simulator. This time click Display Preferences Values This button, you will see:

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.