Android Implementation Remember password login function

Source: Internet
Author: User
Tags gettext

In the Android program, we often use a lot of login features, you will see a lot of places to remember the password. Here, I'll write a simple password-remembering function for everyone. Note that here is a simple remember password function, only for practicing learning with OH. This program is mainly for learning Android friends as an introductory program to learn. The knowledge point used is mainly the IO stream knowledge of java. Therefore, if the knowledge of the IO stream is unfamiliar to friends, be sure to go back to review the knowledge of IO. The knowledge of Io stream is very important for our future development. Here, by the way, the study of Android in the end, rare or java. If the early Java science is good, in fact, Android is still relatively handy to learn.

Here, let's talk about this program first.

The main function of the program is to enter the user account and password, if the user has a tick to remember the password, click the login button and then quit the program, the next time you start the program, will automatically load the account and password. If the user does not tick, it will not load.

Principle: Here we mainly put the user input account and password with IO stream written to the Info.txt file, if you have to remember the password, the next time you start, then use IO stream to read the Info.txt account and password.

Speaking of file read and write problems, by the way, Android, the program can only be "data/data/own program folder" under this path to operate. Folders under other programs are inaccessible.

Let's look at the run and code below. Let's see first.

Here's a look at the code

Layout file Activity_main.xml

<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= "Com.example.mylogin.MainActivity" >    <EditTextAndroid:id= "@+id/et_username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/tips" />    <EditTextAndroid:id= "@+id/et_password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/tips2"Android:inputtype= "Textpassword" />    <RelativelayoutAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" >        <CheckBoxAndroid:id= "@+id/cb_remerber"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:text= "@string/tips3" />        <ButtonAndroid:id= "@+id/bt_login"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:onclick= "Login"Android:text= "@string/login" >        </Button>    </Relativelayout></LinearLayout>

String file Strings.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">MyLogin</string>    <stringname= "Hello_world">Hello world!</string>    <stringname= "Action_settings">Settings</string>    <stringname= "Tips">Please enter user name</string>    <stringname= "TIPS2">Please enter user password</string>    <stringname= "TIPS3">Remember password</string>    <stringname= "Login">Landing</string></Resources>

Java file Mainactivity.java

 PackageCom.example.mylogin;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText UserName, PassWord; PrivateCheckBox Box; File File=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); UserName=(EditText) Findviewbyid (r.id.et_username); PassWord=(EditText) Findviewbyid (R.id.et_password); Box=(CheckBox) Findviewbyid (R.id.cb_remerber); Try{load (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    //Click Login, write the account password method@SuppressLint ("Showtoast")     Public voidLogin (View v)throwsIOException {String name=Username.gettext (). toString (); String pwd=Password.gettext (). toString (); FileOutputStream Fos=NULL; //determine if there is a tick to remember the password        if(box.ischecked ()) {Try {                /** GETFILESDIR () path is actually data/data/project package/files Android, each program can only read and write under its own package. * For example, in this case, the path is actually data/data/com.examle.mylogin/files/info.txt*/file=NewFile (Getfilesdir (), "Info.txt"); FOS=Newfileoutputstream (file); //Converts the name and PWD to byte array writes. # #是为了方便待会分割Fos.write ((name + "# #" +pwd). GetBytes ()); Toast.maketext (mainactivity. This, "Landing success", 0). Show (); } finally {                if(Fos! =NULL) {fos.close (); }            }        } Else {            //If the user does not tick the remember password, it is determined whether the file exists, the existence is deleted            if(File.exists ()) {file.delete (); Toast.maketext (mainactivity. This, "Landing success", 0). Show (); } Else{toast.maketext (mainactivity). This, "Landing success", 0). Show (); }        }    }    //How to load the account password     Public voidLoad ()throwsIOException {fileinputstream Fistream=NULL; BufferedReader BR=NULL; File=NewFile (Getfilesdir (), "Info.txt"); if(File.exists ()) {Try{Fistream=Newfileinputstream (file); /*Convert bytes into a character stream, because we know info.txt * Only one row of data, in order to use the ReadLine () method, so we here * into a character stream, in fact, with the word stream can also be done. But more trouble .*/BR=NewBufferedReader (NewInputStreamReader (Fistream)); //Read Info.txtString str =Br.readline (); //splits the contents of the Info.txt. This is why you should add # #的原因 when writingString arr[] = Str.split ("# #"); Username.settext (arr[0]); Password.settext (arr[1]); } Catch(FileNotFoundException e) {e.printstacktrace (); } finally {                if(BR! =NULL) {br.close (); }            }        } Else {        }    }}

Android Implementation Remember password login function

Related Article

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.