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 ..