Simple implementation of sharepreferences in Android

Source: Internet
Author: User

Android provides sharepreferences, a lightweight data storage model that can store small amounts of data and provide data interfaces for itself and other applications. Sharepreferences is lighter compared to other data storage methods. Here's the code for the entire Sharepreferences implementation:

XML layout file:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        >    <EditTextAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/edittext"Android:width= "200DP"/>    <EditTextAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword"Android:ems= "Ten"Android:id= "@+id/edittext2"/>    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Save"Android:id= "@+id/button"/>    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "New Text"Android:id= "@+id/textview"/>    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "New Text"Android:id= "@+id/textview2"/>    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Display"Android:id= "@+id/button2"/></LinearLayout>

Java main file:

 Packagecom.example.sharepreferencesTest;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classMyActivityextendsActivity {/*** Called when the activity is first created. */    PrivateEditText User; PrivateEditText address; PrivateButton Login; PrivateButton Show; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); User=(EditText) Findviewbyid (R.id.edittext); Address=(EditText) Findviewbyid (R.ID.EDITTEXT2); Login=(Button) Findviewbyid (R.id.button); Login.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//write in SharedpreferencesSharedpreferences User_ino = getsharedpreferences ("User_ino", context.mode_private);//using mode_private modeSharedpreferences.editor Editor =User_ino.edit (); String ename=User.gettext (). toString (); String eaddress=Address.gettext (). toString (); Editor.putstring ("Name", ename); Editor.putstring ("Address", eaddress);            Editor.commit ();        }        }); Show=(Button) Findviewbyid (R.id.button2); Show.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//read the value in SharedpreferencesSharedpreferences User_ino2 = getsharedpreferences ("User_ino", context.mode_private); String name1= user_ino2.getstring ("name", "" "); String Age1= user_ino2.getstring ("Address", "" "); //show values that are read outTextView TV1 =(TextView) Findviewbyid (R.id.textview); TextView TV2=(TextView) Findviewbyid (R.ID.TEXTVIEW2);                Tv1.settext (NAME1);            Tv2.settext (AGE1);    }        }); }}

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.