Android Four click-to-Listen mode

Source: Internet
Author: User

Android Click-to-listen events are often used, often used in the following types of

First: The most commonly used, by implementing Onclicklisten, the implementation of the OnClick method to achieve

Implement the interface to achieve click button1 = (Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (this);


@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.button1:toast.maketext (Mainactivity.this, "Implementing Interface Mode" , 0). Show (); break;}}

The advantage of this method is that when a page has a large number of click events, you can put the click event in the override method, avoid too much code in the OnCreate, inconvenient to read

The second kind: rewrite a class to implement the Onclicklistener interface

Rewrite a class to implement Button2 = (Button) Findviewbyid (R.id.button2); Button2.setonclicklistener (new Mybuttonclick ());

Class Mybuttonclick implements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.butto N2:Toast.makeText (Mainactivity.this, "overriding class mode", 0). Show (); break;}}}

The Third Kind: The method of clicking the click event directly in the layout file, realizes the listener of click event through reflection

public void Btnclick (View v) {        switch (V.getid ()) {case        R.id.button3:            toast.maketext (Mainactivity.this, " Define the click Method directly in the layout file, 0). Show ();            break;        }    }

The fourth kind: Anonymous inner class method to implement

Anonymous inner class        button4 = (Button) Findviewbyid (r.id.button4);        Button4.setonclicklistener (New Onclicklistener () {            @Override public            void OnClick (View v) {                switch ( V.getid ()) {case                r.id.button4:                    toast.maketext (mainactivity.this, "Anonymous inner class", 0). Show ();                    Break;}}        );

See demo,:http://download.csdn.net/detail/xiaoxiaomao123098/9008249 for details.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Four click-to-Listen mode

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.