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); } }); }}