Public Class Mainactivity Extends Activity {@ override Protected Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. activity_main );} Public Void Onclick (view v ){ Switch (V. GETID ()){ Case R. Id. button1: sharedpreferences sp = This . Getsharedpreferences ("myxml" , Context. mode_private); Editor = Sp. Edit (); editor. putstring ( "Name", "nanguabing" ); Editor. putint ( "Age", 20 ); // Persistent operations to storage devices Editor. Commit (); toast. maketext ( This , "SP writing completed", 1 ). Show (); Break ; Case R. Id. button2: sharedpreferences SP1 = This . Getsharedpreferences ("myxml" , Context. mode_private); string name = Sp1.getstring ("name", "nobody" ); Int Age = sp1.getint ("Age", 0 ); Toast. maketext ( This , "Name =" + name + ", age =" + age, 1 ). Show (); Break ; Default : Break ; }}@ Override Public Boolean Oncreateoptionsmenu (menu ){ // Inflate the menu; this adds items to the action bar if it is present. Getmenuinflater (). Inflate (R. Menu. activity_main, menu ); Return True ;}}
File Format:
<? XML version = '1. 0' encoding = 'utf-8' standalone = 'yes' ?> < Map > < String Name = "Name" > Nanguabing </ String > < Int Name = "Age" Value = "20" /> </ Map >
Mobile development QQ group: 59516399
Csdn: