Androidstudio 3.1.4
1. Create a new project, the project name is login, the layout interface is activity_login.xml, open the layout interface for layout
2. Need to use four TextView a plain text one password a button
3. Set button's onclick to Thisbuttonlogin
4. User name edit box ID is set to useredit, password edit box ID is set to Passworedit
5. Open the java\com. Package name. Login\login Writing code
6. The code is as follows
First you need a function to accept the button press event
1 Public void thisbuttonlogin (view view) {2 3 }
Get user-entered account and password content
1 EditText EditText = (EditText) Findviewbyid (r.id.useredit); 2 String user = edittext.gettext (). toString (); 3 EditText = (editText) Findviewbyid (r.id.passwordedit); 4 String pw = Edittext.gettext (). toString ();
Determines whether the contents of two edit boxes are empty
1 if(User = =NULL|| User.length () = = 0) {2Alertdialog.builder usermessage=NewAlertdialog.builder ( This);3Usermessage.setmessage ("User name cannot be empty!") ");4Usermessage.setpositivebutton ("OK", click1);5Alertdialog usermessage1=usermessage.create ();6 usermessage1.show ();7}Else if(PW = =NULL|| Pw.length () = = 0) {8Alertdialog.builder pwmessage=NewAlertdialog.builder ( This);9Pwmessage.setmessage ("Password cannot be empty!") ");TenPwmessage.setpositivebutton ("OK", click1); OneAlertdialog pwmessage1=pwmessage.create (); A pwmessage1.show (); -}
Pop-up message box to display user-entered account number and password
1 // pop-up message box to display user-entered account number and password 2 New Alertdialog.builder (this); 3 Winmessage.setmessage ("Account:" +user+ "\ n" + "Password:" +pw); 4 Winmessage.setpositivebutton ("OK", click2); 5 Alertdialog winmessage2 = winmessage.create (); 6 Winmessage2.show ();
Event triggered when an account or password is empty when the OK button on the message box button is pressed
1 Private New Dialoginterface.onclicklistener () {2 @Override3 Public void int which) {4 dialog.cancel (); 5 }6 };
Enter an event that is triggered when the OK button is pressed on the user input account and password information box button
1 Private New Dialoginterface.onclicklistener () {2 @Override3 Public void int which) {4 android.os.Process.killProcess (Android.os.Process.myPid ()); 5 }6 };
7. The final code effect is as follows
1 PackageCom.shawna.login;2 3 ImportAndroid.content.DialogInterface;4 ImportAndroid.support.v7.app.AlertDialog;5 Importandroid.support.v7.app.AppCompatActivity;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.EditText;9 Ten Public classLoginextendsappcompatactivity { One A @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_login); - } - Public voidthisbuttonlogin (view view) { - //Receive button press event + - //get user-entered account number and password +EditText EditText =(EditText) Findviewbyid (r.id.useredit); AString user =Edittext.gettext (). toString (); atEditText =(EditText) Findviewbyid (r.id.passwordedit); -String PW =Edittext.gettext (). toString (); - - //This section is the legacy of the test, used to output the input account content before judging - //Alertdialog.builder message = new Alertdialog.builder (this); - //message.setmessage (user); in //Message.setpositivebutton ("OK", click1); - //Alertdialog message1 = Message.create (); to //message1.show (); + - //determines whether the contents of two edit boxes are empty the if(User = =NULL|| User.length () = = 0) { *Alertdialog.builder usermessage=NewAlertdialog.builder ( This); $Usermessage.setmessage ("User name cannot be empty!") ");Panax NotoginsengUsermessage.setpositivebutton ("OK", click1); -Alertdialog usermessage1=usermessage.create (); the usermessage1.show (); +}Else if(PW = =NULL|| Pw.length () = = 0) { AAlertdialog.builder pwmessage=NewAlertdialog.builder ( This); thePwmessage.setmessage ("Password cannot be empty!") "); +Pwmessage.setpositivebutton ("OK", click1); -Alertdialog pwmessage1=pwmessage.create (); $ pwmessage1.show (); $}Else{ - //pop-up message box to display user-entered account number and password -Alertdialog.builder Winmessage =NewAlertdialog.builder ( This); theWinmessage.setmessage ("Account:" +user+ "\ n" + "Password:" +PW); -Winmessage.setpositivebutton ("OK", Click2);WuyiAlertdialog Winmessage2 =winmessage.create (); the winmessage2.show (); - } Wu } - PrivateDialoginterface.onclicklistener Click1 =NewDialoginterface.onclicklistener () { About @Override $ Public voidOnClick (Dialoginterface Dialog,intwhich) { - Dialog.cancel (); - } - }; A PrivateDialoginterface.onclicklistener Click2 =NewDialoginterface.onclicklistener () { + @Override the Public voidOnClick (Dialoginterface Dialog,intwhich) { - android.os.Process.killProcess (Android.os.Process.myPid ()); $ } the }; the}
8. After the completion of the writing can be debugged! lucky~
Androidstudio Login Interface