Android sharedpreferences simple application Insert data read data

Source: Internet
Author: User

 Packagecom.sharedpreference; ImportJava.text.SimpleDateFormat; Importjava.util.Date; ImportAndroid.os.Bundle; Importandroid.app.Activity; Importandroid.content.SharedPreferences; ImportAndroid.view.Menu; ImportAndroid.view.View; ImportAndroid.view.View.OnClickListener; ImportAndroid.widget.Button; ImportAndroid.widget.Toast;  Public classSharedpreferenceactivityextendsActivity {sharedpreferences preferences; //Editor put valueSharedpreferences.editor Editor; PrivateButton Btnwrite; PrivateButton Btnread; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.activity_main); Btnread=(Button) Findviewbyid (R.id.read); Btnwrite=(Button) Findviewbyid (r.id.write); //context-provided getsharedpreferences get sharedpreferences instancesPreferences = Getsharedpreferences ("Bang", mode_private); Editor=Preferences.edit (); Btnread.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//reading string data typesString time = preferences.getstring ("Time",NULL); //read int data type                intRandnum = Preferences.getint ("Randrom", 0); String result= Time = =NULL? "You have not yet written the data": "Write Time:" +time+ "\ n the last generated random number is:" +Randnum; Toast.maketext (sharedpreferenceactivity. This, result,5000). Show ();                    }          }); Btnwrite.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy mm month DD Day" + "Hh:mm:ss"); Editor.putstring ("Time", Sdf.format (NewDate ())); Editor.putint ("Randrom", (int) (Math.random () *100)); //commit the saved dataEditor.commit ();                }          }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present. getmenuinflater (). Inflate (R.menu.main, menu); return true; }        }  

The XML file under the generated shared_prefs is

<?XML version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>  <Map>      <stringname= "Time">April 14, 2014 01:10:21</string>      <intname= "Randrom"value= " the" />  </Map>  

Sharedpreferences store simple data Save key value pair type data

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.