sharepreference is used to save data, the main call Context.getsharepreferences (String name, int mode) method to get the Sharepreferences interface, The first parameter of the method is the file name, and the second parameter is the operation mode.
There are three modes of operation:
Mode_private (Private)
Mode_world_readable (Readable)
Mode_world_writeable (writable)
Sharepreference provides methods for obtaining data, such as GetString (String key,string defvalue), etc., Call Harepreferences's edit () method to return the Sharepreferences.editor internal interface, which provides methods for saving data such as:putstring (String key,string value ), call the Commit () method of the interface to Save the data.
As follows:
Main XML code:
<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:backg round= "@drawable/bg_login_activity" android:orientation= "vertical" tools:context= ". Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_conte NT "android:layout_marginleft=" 35dip "android:layout_margintop=" 150dip "android:orientation=" Horizont Al "> <textview android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" User name: "android:textsize=" 20DP "/> <edittext android:id=" @+id/ Username "android:layout_width=" 200DP "android:layout_height=" 35DP "android:background=" @ Drawable/bg_input_center "/> </LinearLayout> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginleft= "35di P "android:layout_margintop=" 8DP "android:orientation=" horizontal "> <textview Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Password:" Android:textsize= "20DP"/> <edittext android:id= "@+id/password" android:layout_width= "200DP" android:layout_height= "35DP" android:background= "@drawable/bg_input_center" Andro Id:password= "true"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" an droid:layout_height= "Wrap_content" android:layout_marginleft= "75dip" android:layout_margintop= "8DP" a ndroid:orientation= "Horizontal" > <textview android:layout_width= "wrap_content" Android : layout_height= "Wrap_conTent "android:text=" Remember Password: "/> <checkbox android:id=" @+id/savepassword "Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> </LinearLayout> <b Utton android:id= "@+id/login_btn" android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:layout_marginleft=" 75dip "android:text=" Landing "/> </LinearLayout> </linearl Ayout>
key functions for saving data to a file:
public void Setuserinfo (string key, String value) { Sharedpreferences sp = context.getsharedpreferences (user_info,< C3/>context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); Editor.remove (key); Editor.putstring (key, value); editor.commit ();
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sharepreference Implement remember Password function