In Android, the listener method of view has different writing methods when anonymous objects are used.
Like other listener methods, onclicklistener is a view-class interface that can be used after being overloaded. Its interface definition is as follows:
Public interface onclicklistener {<br/>/** <br/> * called when a view has been clicked. <br/> * @ Param v the view that was clicked. <br/> */<br/> void onclick (view V); <br/>}Android source code path: Framework/CORE/Java/Android/View/view. Java (Android v2.2)
Listener has a variety of writing methods in use. Understanding these methods has limited benefits for programming, but it is useful for reading code. It can also be used to show off like Kong Yi, but I think it is of great significance for other programmers involved in Android programming to have a deep understanding of Java or Android programming.
This example uses six methods. Due to the flexibility of the Java syntax, it is very likely that a new method is born, so this article will only understand, do not make him your soul chain, which limits your exploration and contribution in the android field. Of course, if you discover new writing methods or create new writing methods, you can also tell me how to learn them together. The following is the program code:
1. Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> <br/> <textview <br/> Android: id = "@ + ID/tvtitle" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "30dip" <br/> Android: layout_gravity = "center" <br/> Android: gravity = "center" <br/> Android: Height = "24dip" <br/> Android: textcolor = "# ff0000" <br/> Android: textsize = "20sp" <br/> Android: TEXT = "show click button" <br/> Android: focusable = "true"> <br/> <requestfocus/> <br/> </textview> </P> <p> <button <br/> Android: id = "@ + ID/button1" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "button1"/> </P> <p> <button <br/> Android: Id = "@ + ID/button 2 "<br/> Android: layout_width =" match_parent "<br/> Android: layout_height =" wrap_content "<br/> Android: TEXT = "button2"/> </P> <p> <button <br/> Android: Id = "@ + ID/button3" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "button3"/> </P> <p> <button <br/> Android: Id = "@ + ID/button4" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "Wrap_content" <br/> Android: text = "button4" <br/> Android: onclick = "btn4onclick"/> <! -- Bind method btn4onclick --> </P> <p> <button <br/> Android: Id = "@ + ID/button5" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "button5"/> </P> <p> <button <br/> Android: Id = "@ + ID/button6" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "button6"/> <br/> </linearlayout>
2. Main. Java
Public class main extends activity implements onclicklistener {<br/> private button m_button1, m_button2, m_button3, m_button4, m_button5, m_button6; <br/> Public textview TV; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> TV = (textview) findviewbyid (R. id. tvtitle); <br/> m_button1 = (button) findviewbyid (R. id. button1); <br/> m_button2 = (button) findviewbyid (R. id. button2); <br/> m_button3 = (button) findviewbyid (R. id. button3); <br/> // m_button4 = (button) findviewbyid (R. id. button4); // XML binding button, Android: onclick = "btn4onclick" <br/> m_button5 = (button) findviewbyid (R. id. button5); <br/> m_button6 = (button) findviewbyid (R. id. button6); </P> <p>/* <br/> * method 1. The parameter This is equivalent to the new onclicklistener () object, that is, the main Class Object <br/> * in this way, the public void onclick method must be written in the main class, And the implements onclicklistener interface must be implemented at the beginning of the main class, that is, this object can directly call the interface method onclick () <br/> */<br/> m_button1.setonclicklistener (this); </P> <p> m_button2.setonclicklistener (clicklistener ); // Method 2: Use the object clicklistener </P> <p> m_button3.setonclicklistener (New button. onclicklistener () {// method 3, which creates a listener using an anonymous object, same method 2, it can be considered as another way of writing <br/> @ override <br/> Public void onclick (view v) {<br/> string strtmp = "Click button03 "; <br/> TV. settext (strtmp); <br/>}< br/>}); <br/> // Method 4: bind the method btn4onclick when creating the XML file. For details, see main. XML </P> <p> m_button5.setonclicklistener (New clicklistener2 (); // Method 5: design a listener class. The Listener method references the method in the onclicklistener interface, create an anonymous object </P> <p> m_button6.setonclicklistener (New callout (this); // Method 6. The external class implements the event listener interface, which is rarely used, for details, see the file callout. java <br/>}</P> <p> @ override <br/> Public void onclick (view v) {<br/> log. I ("log", "click"); <br/> string strtmp = "Click button01"; <br/> TV. settext (strtmp); <br/>}</P> <p> Public onclicklistener clicklistener = new onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> string strtmp = "Click button02"; <br/> TV. settext (strtmp); <br/>}< br/>}; </P> <p> Public void btn4onclick (view) {<br/> string strtmp = "Click button04"; <br/> TV. settext (strtmp); <br/>}</P> <p> public class clicklistener2 implements view. onclicklistener {<br/> @ override <br/> Public void onclick (view v) {<br/> string strtmp = "Click button05"; <br/> TV. settext (strtmp); <br/>}< br/>}; <br/>}
3. calloutClass
Public class callout implements onclicklistener {<br/> private main activity; </P> <p> Public callout (activity) {<br/> This. activity = (main) activity; <br/>}</P> <p> @ override <br/> Public void onclick (view V) {<br/> string strtmp = "Click button06"; <br/> activity. TV. settext (strtmp); <br/>}< br/>}
Run:
Source code download
Reference recommendations:
Android listening Methods
Android programming-listener listening method and implementation principles
Differences between interfaces and abstract class abstract classes and interfaces
Java interface and abstract class