Sharepreference Implement remember Password function

Source: Internet
Author: User

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

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.