Android button listener

Source: Internet
Author: User

It wasn't long before you started to learn Android. you may understand it and write it down as a note here.

It is too simple to create only one button. You always want to use it to communicate with people, so you need to set a listener.

There are two methods. The first method is to set the anonymous internal class, which is also the most common method.

First, add a listener for the button,

Button01.setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Textview01.settext ("button down ");

}
});

There is also a way to write an anonymous internal class:

Onclicklistener monter01 = new view. onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Textview01.settext ("imagebutton down ");
}
};
Imagebutton01.setonclicklistener (monter01 );

This writing method is a bit like an internal class, that is, when monter01 is defined, an anonymous internal class is also used.

The listener adds an onclicklistener interface, and The onclick method must be rewritten in the function body. This is the writing of anonymous internal classes in Java. If you do not understand it, check it.

Although the anonymous internal class is easy to write, it will make the entire code structure unclear. If there are a lot of code to be written in the internal class, we recommend that you do not use this method.

Second: internal class. First, create an internal class to implement the onclicklistener interface. The onclick method must be rewritten in the interface. The Code is as follows. The function body contains your own code.

Class mymonter implements onclicklistener {
@ Override
Public void onclick (view v ){
Textview01.settext ("imagebutton down ");
}
}

Here mymonter is an internal class. If you use this internal class, bind it to the button. There are two methods.

1. Declare a mymonter object and then pass it to the button. Code:

Onclicklistener monter01 = new mymonter ();
Imagebutton01.setonclicklistener (monter01 );

II. Create a new mymonter directly. Code:

Imagebutton01.setonclicklistener (New mymonter );

Both methods can be used ..

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.