10.1 sharedpreferences Overview
There are five kinds of data storage, the previous one is to store the data by the IO stream as a file, the Sharedpreferences way to learn to save the data, the main saving is the user's preferences.
Many times, the program we develop is required to provide the user with software parameter setting function. Users according to their interests and hobbies of the software to configure the parameters to comply with their own habits.
For example, when we use eclipse, we can set the font display color, size, and so on. Within eclipse, XML-formatted files are used to store configuration parameters for the software.
If we want to save the various parameters that the user sets on the software in Android, the way is sharedpreferences, and the internal XML file is used to save the parameters, but for us, we don't need to deal with the XML file directly, Because Sharedpreferences has encapsulated the API for manipulating XML files.
This data storage method is specifically used to do this.
10.2 Project Requirements
The requirements of the project are consistent with the Nineth chapter, only the user name and password are saved, and the method used is sharedpreferences
10.3 activity_main.xml File
<linearlayout xmlns: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= "Com.example.logintest.MainActivity" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter user name"android:textsize= "18DP"/> <EditText Android:id= "@+id/et_username"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:textsize= "18DP"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter password"android:textsize= "18DP"/> <EditText Android:id= "@+id/et_pwd"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Numberpassword"android:textsize= "18DP"/> <relativelayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" > <CheckBox Android:id= "@+id/cb_checked"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "true"Android:text= "Remember Password"android:textsize= "18DP"/> <Button Android:id= "@+id/bt_login"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:text= "Login"android:textsize= "18DP"/> </RelativeLayout> </LinearLayout>
10.4 Mainactivity.java File
Packagecom.example.sharedperences;Importandroid.support.v7.app.ActionBarActivity;Importandroid.text.TextUtils;Importandroid.app.Activity;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateEditText Musername; PrivateEditText Muserpassword; PrivateButton Mlogin; PrivateSharedpreferences sp; PrivateEditor Editor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Musername=(EditText) Findviewbyid (r.id.et_username); Muserpassword=(EditText) Findviewbyid (R.ID.ET_PWD); Mlogin=(Button) Findviewbyid (R.id.bt_login); Mlogin.setonclicklistener ( This); SP= This. Getsharedpreferences ("UserInfo", mode_private); Musername.settext (Sp.getstring ("UserName",NULL)); Muserpassword.settext (Sp.getstring ("Usernumber",NULL)); } @Override Public voidOnClick (View v) {//TODO auto-generated Method StubString name=Musername.gettext (). toString (). Trim (); String Number=Muserpassword.gettext (). toString (). Trim (); if(Textutils.isempty (name) | |textutils.isempty (number)) {Toast.maketext ( This, "User name or password is empty", 1). Show (); return; } Else{Editor=Sp.edit (); Editor.putstring ("UserName", name); Editor.putstring ("Usernumber", number); Editor.commit (); } }}
(10) data storage and access in Android-save data using sharedpreferences