Read and Write sharedpreferences on the third day of Android Development

Source: Internet
Author: User
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:

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.