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