Multiple ways to listen for Android listener

Source: Internet
Author: User
Tags anonymous event listener

In Android, the listener method of view has different ways to use anonymous anonymous objects.

Onclicklistener, like other listener methods, is the interface of the view class, which can be used after the overload is implemented, and its interface is defined as follows:

Public interface 

Onclicklistener {     
    /**    
     * Called when a view has been.    
     *    
     * @param v The view that is clicked.    
     * 
    /void OnClick (View v);     
}

Android Source path: Framework/core/java/android/view/view.java (Android v2.2)

Listener in the use of a variety of writing, to understand these, the programming benefits are limited, but the reading code is very useful. You can probably show off like Kong Yi already, but I think it's important for other programmers in the early days of Android to get to know Java or Android programming in depth.

This example uses six methods, because the flexibility of Java syntax, it is possible to think, a new method is born, so this article only to understand, do not let him become your soul chain, leading to limit you in the field of Android to do more in-depth and broader exploration and contribution. Of course, if you find a new writing or create a new writing, you can also tell me, we learn together. Here's The program code:

1, Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertic Al "> <textview android:id=" @+id/tvtitle "android:layout_width=" Fill_parent "and roid:layout_height= "30dip" android:layout_gravity= "center" android:gravity= "center" Android:hei  
        ght= "24dip" android:textcolor= "#ff0000" android:textsize= "20SP" android:text= "Show Click button" Android:focusable= "true" > <requestfocus/> </TextView> <bu Tton android:id= "@+id/button1" android:layout_width= match_parent "android:layout_height=" Wrap_ Content "android:text=" Button1 "/> <button android:id=" @+id/button2 "a Ndroid:layout_width= "MatCh_parent "android:layout_height=" wrap_content "android:text=" Button2 "/> <butt On android:id= "@+id/button3" android:layout_width= "match_parent" android:layout_height= "Wrap_co" Ntent "android:text=" Button3 "/> <button android:id=" @+id/button4 "and Roid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Button4" an droid:onclick= "Btn4onclick"/> <!--binding method Btn4onclick--> <button android:id= "@+id/but Ton5 "android:layout_width=" match_parent "android:layout_height=" "Wrap_content" Bu  
        Tton5 "/> <button android:id= @+id/button6" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Button6"/> </LinearLayout>

2, Main.java

public class Main extends activity implements Onclicklistener {private Button m_button1, M_button2, M_button3     
    , M_button4, M_button5, M_button6;     
         
    Public TextView TV;     
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     

        Setcontentview (R.layout.main);     
        TV = (TextView) Findviewbyid (r.id.tvtitle);     
        M_button1 = (Button) Findviewbyid (R.id.button1);     
        M_button2 = (Button) Findviewbyid (R.id.button2);     
M_button3 = (Button) Findviewbyid (R.id.button3);    M_button4 = (Button) Findviewbyid (R.ID.BUTTON4);     
        XML binding button,android:onclick= "Btn4onclick" m_button5 = (Button) Findviewbyid (R.ID.BUTTON5);     
    
        M_button6 = (Button) Findviewbyid (R.ID.BUTTON6); /* Method 1, parameter this is the equivalent of the new Onclicklistener () object, which is the class Main object * In this way, the public void OnClick method must be written in the In the main class, and MaiThe beginning of the N class implements the implements Onclicklistener interface, where the This object can call the interface method onclick ()/M_button1.setonclicklistener (thi     

        s);        M_button2.setonclicklistener (Clicklistener); Method 2, using the object Clicklistener M_button3.setonclicklistener (new Button.onclicklistener () {//Method 3, using an anonymous object to create a listener  , same method 2, can be considered as another way of writing @Override public void OnClick (View v) {String strtmp =     
                "Click Button03";     
            Tv.settext (strtmp);     
        }     
        });     Method 4, using the XML file creation-time binding method Btn4onclick, see Main.xml M_button5.setonclicklistener (New ClickListener2 ());        Method 5, you design a listener class, the listener method refers to the method in the Onclicklistener interface, creating an anonymous object M_button6.setonclicklistener (new CallOut (this));     
        Method 6, the external class implements the event listener interface, rarely used, see file Callout.java} @Override public void OnClick (View v) {     
        LOG.I ("Log", "click");     
        String strtmp = "click Button01"; Tv.settext (strtmp); Onclicklistener Clicklistener = new Onclicklistener () {@Override public void OnC     
            Lick (View v) {String strtmp = "click Button02";     
        Tv.settext (strtmp);     

    }     
    };     
        public void Btn4onclick (view view) {String strtmp = "click Button04";     
    Tv.settext (strtmp); The public class ClickListener2 implements View.onclicklistener {@Override public void Oncl     
            Ick (View v) {String strtmp = "click Button05";     
        Tv.settext (strtmp);     
}     
    }; }

3, CallOut class

public class CallOut implements 

Onclicklistener {     
    private Main activity;     

    public CallOut [activity activity] {     
        this.activity = (Main) activity;     
    }     

    @Override public 
    void OnClick (View v) {     
        String strtmp = "click Button06";
        Activity.tv.setText (strtmp);     
    }     

Run Effect chart:

SOURCE Download: http://download.csdn.net/detail/sunboy_2050/4249099

Related Article

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.