Simple data Persistence Save (sharedpreferences) for Android small function implementation

Source: Internet
Author: User
Tags gettext



To save some simple configurations, iOS-like Nsuserdefault and COCOS2DX ccuserdefault,android provide sharedpreferences.


A layout
Look first:




Open the Main.xml to modify the contents as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "    ; <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/I Nput_name "/> <edittext android:layout_width=" fill_parent "android:layout_height=" Wrap_content "and Roid:id= "@+id/input_name"/> <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_cont Ent "android:text=" @string/input_age "/> <edittext android:layout_width=" Fill_parent "android:layou t_height= "wrap_content" android:numeric= "integer" android:id= "@+id/input_age"/> <button android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/button_save" and Roid:onclick= "Save" andRoid:id= "@+id/button_save"/></linearlayout>   


Two definition strings
Open Strings.xml Add content as follows:
    <string name= "Input_name" > Name </string>    <string name= "Input_age" > Age </string>    < String name= "Button_save" > Save parameters </string>    <string name= "Success" > Save success </string>    < String name= "fail" > Save failed </string>


Three-function implementation
Modify the Mainactivity.java code as follows:
    Private EditText Nametext;    Private EditText Agetext;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Nametext = (EditText) This.findviewbyid (r.id.input_name);        Agetext = (EditText) This.findviewbyid (r.id.input_age); Read the saved value//sharedpreferences preferences = This.getpreferences (context.mode_private);//Use the class name as the filename by default//shar        Edpreferences preferences = this.getsharedpreferences ("preference", context.mode_private);        Sharedpreferences preferences = Getapplicationcontext (). Getsharedpreferences ("preference", context.mode_private);        String name = preferences.getstring ("name", "null");        String age = string.valueof (Preferences.getint ("age", 0));        Nametext.settext (name);    Agetext.settext (age);        public void Save (View v) {String name = Nametext.gettext (). toString (); Integer age = integer.valueof (agetext. GetText (). toString ()); try{//The first parameter is a file name, you cannot specify a suffix name, the second parameter is the file operation mode sharedpreferences preferences = Getapplicationcontext (). GetS            Haredpreferences ("preference", context.mode_private);            Sharedpreferences.editor Editor = Preferences.edit ();            Editor.putstring ("name", name);            Editor.putint ("Age", age);            Editor.commit ();        Toast.maketext (Getapplicationcontext (), r.string.success, Toast.length_long). Show ();            } catch (Exception e) {toast.maketext (Getapplicationcontext (), R.string.fail, Toast.length_long). Show ();        E.printstacktrace (); }    }


Four Run results

Run results for the first time


Enter the contents, click the Save Parameters button, exit the program, open again,



Simple data Persistence Save (sharedpreferences) for Android small function implementation

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.