Teach my Apprentice Introduction to Android Development (II)

Source: Internet
Author: User

Objective:

The previous period achieved a simple QQ login effect, this period continues to expand the previous issue

Knowledge points in this issue:

Toast Popup, three ways to implement button click event Monitoring

Body:

Toast Pop-up is simple, make a toast in Android studio, and then press TAB to quickly generate a toast

Toast. Maketext (currentactivity.   Toast.  Length_short). Show ();
The three parameters in parentheses, the first is the context (context, where the toast is displayed), the second is a string string, which is what the toast shows, and the third is the time the toast is displayed, short is the meaning

This will enable the popup toast, I changed the previous popup dialog code into the following code, this refers to the current activity, so that can be omitted to write

About listening to events, maybe I didn't explain it too clearly, take the example above , we let the current activity implemented a listening interface, and then copy its OnClick method, through Findviewbyid find the button instance, Call Setonclicklistener to bind the listener , and then, when the user taps the button, it goes into the OnClick method, By V.getid the ID of the view that the current user clicked, and then into a switch branch statement, the ID is the same as R.id.button, and the branch is executed, that is, a toast is popped, not just the button, the other TextView, LinearLayout the control or the root layout can also set the listener's

I've summed up five ways to implement a time listener, which may not be correct, but it's okay to know the way, not to dwell on the details.

Ways to implement event listeners:

    • Inner class Form
    • External class Form
    • Activity itself as an event listener class
    • Anonymous inner class form
    • Bind directly to a label

Inner class form

Defines the event listener class within the current class

  

PublicClass TestExtendsactivity{@OverrideProtectedvoid OnCreate (@Nullable Bundle savedinstancestate) { Span style= "color: #0000ff;" >super.oncreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Button button =new Mybuttonlistener (); Button.setonclicklistener (listener); } class mybuttonlistener implements view.onclicklistener{@Override public void OnClick (View v) {// related event handling             /span>   

Class form of outer class

Test class

Extends activity{    @Override    voidSuperonCreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Button button =new mybuttonlistener (); Button.setonclicklistener (listener);}}     

Mybuttonlistener class

Implements View.onclicklistener {    @Override    void// event handling }}    

activity itself as an event listener class

The activity itself as the event listener class

  

PublicClass Testextends Activity implements  view.onclicklistener{@Override protected void OnCreate (@Nullable Bundle savedinstancestate) {.oncreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Button button =this); } @Override public void// event handling             /span>    

anonymous inner class form
Publicclass Test extendsprotected void OnCreate (@Nullable Bundle savedinstancestate) {super . OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Button button =new View.onclicklistener () {@Override public void//  event handling  

bind directly to a label

Add the onclick attribute to the XML layout file and set the relevant method

Define a method, remember that the parameter is the view

Want to know more about this one can also look at this: Android development based on the monitoring of event processing

Teach my Apprentice Introduction to Android Development (II)

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.