6th chapter, edit box EditText and Button buttons (learn Android from scratch)

Source: Internet
Author: User

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)

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.