Sharedpreferences is used to store data in the form of key-value pairs in the current application-specific storage space
Packagecom.itheima.sharedpreferences;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_username; PrivateEditText Et_password; PrivateCheckBox Cb_isautologin; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_username=(EditText) Findviewbyid (r.id.et_username); Et_password=(EditText) Findviewbyid (R.id.et_password); Cb_isautologin=(CheckBox) Findviewbyid (R.id.isautologin); Readaccount (); } //If a user has previously stored information on the phone, the user information needs to be echoed Private voidReadaccount () {sharedpreferences spreferences= Getsharedpreferences ("UserInfo", mode_private); String username=spreferences.getstring ("username", "" "); String Password= spreferences.getstring ("Password", "" "); Et_username.settext (username); Et_password.settext (password); } //log in to save the user name and password to the internal storage space Public voidLogin (View v) {String username=Et_username.gettext (). toString (); String Password=Et_password.gettext (). toString (); if(Textutils.isempty (username) | |textutils.isempty (password)) {Toast.maketext ( This, "The user name or password cannot be empty", Toast.length_short). Show (); return; } if(cb_isautologin.ischecked ()) {//file name does not write extension, XML extension is added automatically /** Name desired preferences file. * If a Preferences file by this name does not exist, it'll be created * When you retrieve an editor (Sha Redpreferences.edit ()) and then commit changes (Editor.commit ()). */sharedpreferences spreferences= Getsharedpreferences ("UserInfo", mode_private); Editor Editor=Spreferences.edit (); Editor.putstring ("Username", username); Editor.putstring ("Password", password); Editor.commit (); } toast.maketext ( This, "Login Successful", 0). Show (); } }
The file storage structure is as follows:
Layout file:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <EditTextAndroid:id= "@+id/et_username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/_username" /> <EditTextAndroid:id= "@+id/et_password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/_password"Android:inputtype= "Textpassword"/> <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <CheckBoxAndroid:id= "@+id/isautologin"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:text= "@string/_autologin"android:layout_centervertical= "true"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:text= "@string/_login"Android:onclick= "Login"/> </Relativelayout></LinearLayout>
Android Basics-Key-value pair storage (sharedpreferences)