Use Sharedpreferences to save information in Android

Source: Internet
Author: User

Package Com.example.sharepreferen;import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class MainActivity Extends Actionbaractivity implements Onclicklistener{private Button btnsave;private EditText etcontent;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Btnsave = (Button) Findviewbyid (r.id.btn_save); etcontent = (EditText) Findviewbyid (R.id.et_ content);//Get sharedpreferences value sharedpreferences preferences = this.getsharedpreferences ("config", Context.mode_ PRIVATE); String content = preferences.getstring ("Content", ""), if (!content.trim (). Equals ("")) {Etcontent.settext (content);} Btnsave.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case R.id.btn_save:save (Etcontent.gettext (). toString ()); Break;default:break;}} public void Save (String content) {//Get sharedpreferences and edit sharedpreferences preferences = This.getsharedpreferences ("config", context.mode_private); Editor editor = Preferences.edit (); Editor.putstring ("Content", content);//Remember to submit editor.commit (); Toast.maketext (This, "Save succeeded", 0). Show ();}}

  

Some of the setup information in the phone is saved in it.

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.