Sharedpreferences Implement remember Password function

Source: Internet
Author: User

Sharedperferences Brief Introduction
    • Used to save simple key-value pairs of data;
    • It puts the data in the/data/data/<package name>/shared_prefs directory, and uses the XML file to save the map key value pairs ;
sharedperferences Use stepsTo store the data in Sharedperferences:

  1. Get the Sharedperference object first: (Three methods)

1). Using the getsharedpreferences () method in the context class, it receives two parameters, the first parameter is the file name, and the second parameter is the operation mode.

Operating mode Mode_pravite: Only the current program can read and write to this file. Mode_multi_process: Read and write to the same file in multiple processes.

Such as:

Sharedpreferences SPF = getsharedpreferences ("Data", context.mode_private);

2). Using the Getpreferences () method in the activity class, it receives only one parameter-the operation mode, and the currently active class name is the file .

Such as:

Sharedpreferences SPF = getpreferences (mode_private);

3). Using the Getdefaultsharedpreferences () method in the Preferencemanager class, it receives a context parameter and names the file with the package name as a prefix .

Such as:

Sharedpreferences SPF = preferencemanager.getdefaultsharedpreferences (this);

  2. Get the Sharedpreferences.editor object again:

Use the edit () method of the Sharedpreferences object.

Sharedpreference.editor Editor = Spf.edit ();

  3. Start adding data:

Writes data as a key-value pair.

Editor.putint ("Age",editor.putstring ("name", "Visen"), Editor.putboolean ("Singledog",  False)

  4. Submit the action:

Editor.commit ();

To read data from the sharedperferences:

Provides a series of get methods for reading data. Such as:

String name = editor.getstring ("name", "");

  If the value for the key does not exist, the default value for the setting is filled in.

Simple Save password function

Login.xml Login Layout Page

<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/black"Android:stretchcolumns= "1" > <LinearLayout android:layout_height= "Wrap_content"Android:background= "@color/black"android:orientation= "Vertical" > <ImageView android:layout_width= "Match_parent"Android:layout_height= "240DP"android:src= "@drawable/image1"/> <TextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/title"android:textsize= "40SP"Android:textcolor= "@color/red"android:gravity= "Center"Android:background= "@color/cyan"/> </LinearLayout> <TableRow android:layout_margintop= "30DP" > <TextView android:layout_height= "Wrap_content"Android:text= "@string/account"android:textsize= "30SP"Android:textcolor= "@color/white"/> <EditText Android:id= "@+id/account"Android:layout_height= "Wrap_content"Android:inputtype= "Text"android:textsize= "20SP"Android:textcolor= "@color/red"android:gravity= "Center"Android:singleline= "true"/> </TableRow> <TableRow> <TextView android:layout_height= "Wrap_content"Android:text= "@string/password"android:textsize= "30SP"Android:textcolor= "@color/white"/> <EditText Android:id= "@+id/password"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword"android:textsize= "20SP"Android:textcolor= "@color/red"android:gravity= "Center"/> </TableRow> <tablelayout android:layout_height= "Wrap_content"Android:stretchcolumns= "0" > <TableRow> <CheckBox Android:id= "@+id/saveselect"Android:background= "@color/red"android:layout_gravity= "End"/> <TextView android:layout_height= "Wrap_content"Android:text= "@string/saveselect"android:textsize= "20SP"Android:textcolor= "@color/white"android:gravity= "Center"android:layout_gravity= "Bottom"/> </TableRow> <TableRow> <Button android:layout_height= "Wrap_content"Android:id= "@+id/login"android:gravity= "Center"Android:layout_span= "2"Android:text= "@string/login"android:textsize= "25SP"Android:textcolor= "@color/red"Android:background= "@drawable/black_bt"/> </TableRow> </TableLayout></TableLayout>

Login.java

 Public classLoginextendsappcompatactivity {PrivateSharedpreferences SPF; PrivateSharedpreferences.editor SPFE; Private intnum = 0; PrivateEditText account =NULL; PrivateEditText PassWord =NULL; PrivateCheckBox Saveselect =NULL; PrivateButton login =NULL ; @Overrideprotected voidonCreate (Bundle saveinstancestate) {//Loading Layouts        Super. OnCreate (saveinstancestate);        Setcontentview (R.layout.login); //Initializing the controlAccount =(EditText) Findviewbyid (R.id.account); PassWord=(EditText) Findviewbyid (R.id.password); Saveselect=(CheckBox) Findviewbyid (r.id.saveselect); Login=(Button) Findviewbyid (R.id.login); //using the context getsharedpreferences (String name,int mode) method to obtain the Sharedpreferences object;        SPF = getsharedpreferences ("Data" , context.mode_private); //use the edit () method of the Sharedpreferences object to get the Sharedpreferences.editor object;        SPFE = Spf.edit (); //check box is selected, if selected, save account, to recover data        if(Spf.getboolean ("Isselect",false)){//check flag, default value is FalseString acc =spf.getstring("Account", "" "); String pas=spf.getstring("PassWord", "" ");            Account.settext (ACC);            Password.settext (PAS); Saveselect.setchecked (true); }        //Set the login button to listen for eventsLogin.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Confirm Account Password                if(Account.gettext (). ToString (). Equals ("Visen") && Password.gettext (). ToString (). Equals ("dsy402645063!")){                    //check box is checked, if checked, you need to save your account and log in, otherwise log in directly and do not save the account                    if(saveselect.ischecked ()) { savedate (); }Else{ spfe.clear ();                    spfe.commit (); }                                        //page JumpIntent Intent =NewIntent (Login. This, Mainactivity.class);                    StartActivity (Intent);                Finish (); }Else{//incorrect account or passwordToast.maketext (Login. This, "Account or password is invalid", Toast.length_short). Show ();    }            }        }); }     Public voidsavedate () {//read the contents of the EditTextString acc =Account.gettext (). toString (); String pas=Password.gettext (). toString (); //Save Data        spfe.putstring ("Account", ACC);        Spfe.putstring ("PassWord", PAS); Spfe.putboolean ("Isselect",true ); //Submit spfe.commit (); } @Override Public voidonbackpressed () {num++; if(num = = 2){            Super. onbackpressed (); }Else{toast.maketext (Login. This, "Press again to exit the program", Toast.length_short). Show (); }    }}

Sharedpreferences 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.