Android programming-listener listening method and implementation principles

Source: Internet
Author: User
When I wrote this question, I suddenly remembered the style of Kong Yi in Lu Xun's pen, which is quite a bit of a chew-on style. Although I have been engaged in mobile phone programming for many years, I have been using C and C ++ programming. Because Android only supports Java development in the early days, for the current popular Android system, I have never been here. Now, due to work needs to begin to study Android programming, due to the main use of C language in the past, at first glance Java, there are many problems in the way of thinking, writing, and programming specifications. A listener method is a variety of writing methods when anonymous objects are used. Like other listener methods, onclicklistener is an abstract interface of the View class. It can be used after being overloaded. Its definition is as follows:
// Compiled from view. Java (version 1.5: 49.0, no super bit)
Public abstract static interface Android. View. View $ onclicklistener {

// Method descriptor #4 (landroid/View/view;) V
Public abstract void onclick (Android. View. View arg0 );

Internal class:
[Internal class information: #1 Android/View/view $ onclicklistener, external class information: #7 Android/View
Internal name: #9 onclicklistener; access flag: 1545 public abstract static]
}
This is an abstract interface definition, which can be derived like a class in use. Abstract Interface) and abstract class are different from C and C ++, but they are similar but different in Java, the probability of this aspect can be learned from Java programming. c ++ programmers may feel overwhelmed by the two and do not know whether to create abstract interfaces or abstract classes for some implementations. This may require some practical code experience to better grasp.
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:

Create an android project using eclipse. Assume that the work name uses button4 and the package uses COM. mypack: Add 6 buttons in the window ., use the default name. The system will automatically name button1 to button6, and then add an edittext. The system will automatically name the edittext1. project package names are free of choice.

The main. xml file of the resource is as follows::

<?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="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button1" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button2" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button3" />    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button4"        android:onClick="Btn4OnClick" />    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button5" />    <Button        android:id="@+id/button6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button6" />    <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:text="click button">        <requestFocus />    </EditText></LinearLayout>

The content of the button4activity. Java file is:

Package COM. mypack; import COM. mypack. r; import android. app. activity; import android. OS. bundle; import android. util. *; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import COM. mypack. callout; public class button4activity extends activity implements onclicklistener {/** called when the activity is first created. */private button m_button1, m_button2, m_button3, m_button4, m_button5, m_button6; Public edittext et; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); 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); m_button5 = (button) findviewbyid (R. id. button5); m_button6 = (button) findviewbyid (R. id. button6); ET = (edittext) findviewbyid (R. id. edittext1);/** method 1, where this is equivalent to the new onclicklistener () object, that is, an object in class test. * if you want to use this method, the public void onclick method must be written in the test class, and implements * onclicklistener is used at the beginning. That is, this object can directly call this method */m_button1.setonclicklistener (this); // method 2, use the object clicklistenerm_button2.setonclicklistener (clicklistener); // method 3. Use an anonymous object to create a listener. The same method can be seen as another method m_button3.setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {string strtmp = "Click button03"; et. settext (strtmp) ;}}); // Method 4, bind method btn4onclick // Method 5 when creating an XML file. Design a listener class by yourself, the method of the listener references the method in the onclicklistener interface, and creates an anonymous object m_button5.setonclicklistener (New clicklistener2 (); // Method 6. The external class implements the event listener interface, which is rarely used, for details, see the file callout. javam_button6.setonclicklistener (New callout (this) ;}@ overridepublic void onclick (view v) {log. I ("log", "click"); string strtmp = "Click button01"; et. settext (strtmp);} public onclicklistener clicklistener = new onclicklistener () {public void onclick (view v) {log. I ("log", "click"); string strtmp = "Click button02"; et. settext (strtmp) ;}}; public class clicklistener2 implements view. onclicklistener {public void onclick (view v) {log. I ("log", "click"); string strtmp = "Click button05"; et. settext (strtmp) ;}}; public void btn4onclick (view) {string strtmp = "Click button04"; et. settext (strtmp );}}

The last button in the file uses the class callout. The code for callout. Java is as follows:

Package COM. mypack; import android. app. activity; import android. view. view; import android. view. view. onclicklistener; import COM. mypack. button4activity; public class callout implements onclicklistener {private button4activity Act; Public callout (activity) {act = (button4activity) activity;} @ override public void onclick (view V) {string strtmp = "Click button06"; Act. ET. settext (strtmp );}}

This article is complete. References:

Http://www.eefocus.com/bbs/article_867_193559.html

Http://blog.csdn.net/wyw1213/article/details/6282277

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.