Our life will encounter a variety of login interface, you need to enter your account and password, like landing qq Ah, all kinds of software need this design, then we come to realize it today ~ ~
First, the interface is designed as follows:
As you can see, we need two editable text boxes to enter the user name and password, and we want to prompt for the type to be entered, using hint to prompt; we need a checkbox to determine whether we choose to save the password and a login button; Because the entire linear layout is vertical, So two editable text boxes, a checkbox, and a button in the vertical direction, which is not very nice, so we need to establish a relative layout so that the checkbox and the button are arranged in one line. The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "Vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:context= "Com.administrator.login.login"Tools:showin= "@layout/activity_login" > <EditText Android:id= "@+id/musername"Android:hint= "User Name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> <EditText Android:layout_below= "@+id/musername"Android:id= "@+id/mpassword"Android:hint= "Password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> <relativelayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > <CheckBox android:layout_centervertical= "true"Android:id= "@+id/mcheckbox"Android:text= "Save Password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/mbutton"Android:text= "Login"Android:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </RelativeLayout></LinearLayout>
After designing the interface, we will implement the activity:
There are several steps required to implement the user login interface:
1. Because the activity will invoke the OnCreate () function as soon as it is started, the OnCreate () function gets the individual controls in the interface through the Findviewbyid () function.
2, set a listener for the button, when the button is clicked and Tick checkbox mcheckbox.ischecked (), get the user name and password, through the output stream output to the internal storage of the Info.txt file, in order to facilitate reading, we set # # To split the user name and password.
3, whether there is no check checkbox, only click on the button, we create a toast prompt interface, the parameters are: context (that is, the current activity), the string to output and the output time (3s or 5s)
4, Save password function realizes is when the user exits the program, once again enters, has displayed the user name and the password, we through the Readfrominfo () method realizes.
5, Readfrominfo (), in order to facilitate the operation of the data, we first have to convert the file stream into the input flow into a character stream, so that we can call the character stream of the ReadLine () function (that is, the implementation of read-by-line), if the file has more than one line, then set the loop while ( The next line is not empty) {executes ReadLine ()}, so we read the information in the info.txt into the string, we put the string in the # #分割的各部分存入字符串数组中, then the array [0] is the user name, array [1] is the password.
6, because we will execute the OnCreate () function when we enter the software, we are going to call the Readfrominfo () function in this case, but if the user is logged in for the first time and does not generate Info.txt, call Readfrominfo directly () The function is definitely going to go wrong, so in the Readfrominfo () function, we want to judge whether the Info.txt file exists file.exits ()
The code is as follows:
PackageCom.administrator.login;ImportAndroid.os.Bundle;ImportAndroid.support.design.widget.FloatingActionButton;ImportAndroid.support.design.widget.Snackbar;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;ImportAndroid.view.View;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.io.InputStreamReader; Public classLoginextendsappcompatactivity {PrivateEditText Musername; PrivateEditText Mpassword; String username=NULL; String Password=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_login); Toolbar Toolbar=(Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (toolbar); Button Mbutton=(Button) Findviewbyid (R.id.mbutton); Musername=(EditText) Findviewbyid (r.id.musername); Mpassword=(EditText) Findviewbyid (R.id.mpassword); FinalCheckBox mcheckbox=(CheckBox) Findviewbyid (R.id.mcheckbox); Readfrominfo (); //invoke read internal storage functionMbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(mcheckbox.ischecked ()) {username=Musername.gettext (). toString (); Password=Mpassword.gettext (). toString (); File File=NewFile (Getfilesdir (), "info.txt");//write internal storage to the Info.txt fileFileOutputStream fos=NULL;//Create an output stream Try{fos=Newfileoutputstream (file); } Catch(FileNotFoundException e) {e.printstacktrace (); } Try{fos.write (username+ "# #" +password). GetBytes ());//the username# #password按位写入文件中Fos.close ();//Close the stream}Catch(IOException e) {e.printstacktrace (); }} toast.maketext (login. This, "Landing success", Toast.length_short). Show ();//The interface of the toast, the parameters are prompted to send activity, prompt content, prompt length } }); } //read internal storage functions Public voidReadfrominfo () {File file=NewFile (Getfilesdir (), "Info.txt"); if(File.exists ()) {BufferedReader br=NULL; Try{br=NewBufferedReader (NewInputStreamReader (NewFileInputStream (file));//Convert bytes to input stream and then into a character stream}Catch(FileNotFoundException e) {e.printstacktrace (); } String str=NULL; Try{str=Br.readline (); } Catch(IOException e) {e.printstacktrace (); } String [] up= Str.split ("# #");//Press the string # #分割的部分依次存入字符串型数组up中Musername.settext (up[0]);//fill in username and passwordMpassword.settext (up[1]); } }}
Output Result:
Design implementation of Android--login user login