In one application, login is often used, below we learn how to develop a login window. We need to learn The basic controls in Android:(1) EditText edit box,(2) button.
First, the Design login window
Open"Res/layout/activity_main.xml"file.
1, respectively, from the toolbar to theActivityDrag out2aEditText(fromText Fields),1a button (fromForm Widgets).
2, openActivity_main.xmlfile.
The code is automatically generated as follows: note①and the②Although the sameEditText, but②to enter a password,android:inputtype= "Textpassword".
3, we modified the above code to the following code, specifically:EditText1intoUserName;eidtText2intoPassWord;Buttion1intoLogin. Text of the login button:android:text= "button"into"Login".
4 , the interface is as follows:
now running the program, already on the phone looks much like a login window. However, we clicked the sign in button, but nothing happens. Below we learn how to add a Click event on the login button.
Second, click event
Open "Src/com.genwoxue.edittextbutton/mainactivity.java" file.
Then enter the following code:
In the above code, we focus on the analysis with a light blue background section, the other is the simplest basic code, if you do not understand, please refer to the previous chapter content.
1 , Section ① Part
Import 5 a package.
2 , Section ② Part
Declares a three control variable.
3, Section③Part
This PartFindviewbyid ()method is a key, and this method represents theR.javafile, find aView(Note: We can put controls andacitivityare treated as aView). For example,tvusername= (EditText) Super.findviewbyid (r.id.username)that we are fromRfile found inUserNamethe control represented last returned toTvusername, the next step we can passTvusername.gettext ()method to obtain its value further.
Another key is to give"Login"button to add a click Listener event:Btnlogin.setonclicklistener (Newloginonclicklistener ()).
4, Section④Part
we create a new classLoginonclicklistenerInheriting InterfacesOnclicklistenerused to implement a click event listener.
Toast.maketext (Getapplicationcontext (), Info,toast.length_short). Show ()the user name and password used to prompt for input.
The effect is as follows:
6th chapter, edit box EditText and Button buttons (learn Android from scratch)