How to implement the listener of Android programming _android

Source: Internet
Author: User

This article is an example of how to implement the listener of Android programming. Share to everyone for your reference, specific as follows:

1, through the internal class implementation listener, the event listener defined as the current internal class, the use of internal classes in the current class can be reused listeners.

The first step: Write Listener class, overlay corresponding abstract method
Step Two: Create the Listener Object
Step Three: Register

Take a button for example, the code is as follows:

Package CN.CSDN.ANDROID.TESXT;
Import android.app.Activity;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Testlisteneractivity extends activity {
Button btn;
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Findviews ();
Myonclicklistener listener = new Myonclicklistener ();
Btn.setonclicklistener (listener);
}
private void Findviews () {
btn = (Button) This.findviewbyid (r.id.button1);
}
Class Myonclicklistener implements Onclicklistener {
@Override public
void OnClick (View v) {
Toast.maketext (testlisteneractivity.this, "xxx", Toast.length_short)
. Show ();}}


2, anonymous internal class is the most extensive form of event listening, because most event listeners only temporary use, so the use of anonymous classes more appropriate, the code is as follows:

Package CN.CSDN.ANDROID.TESXT;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Testlisteneractivity extends activity implements Onclicklistener {
Button btn;
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Findviews ();
Btn.setonclicklistener (this);
}
private void Findviews () {
btn = (Button) This.findviewbyid (r.id.button1);
}
@Override public
void OnClick (View v) {
toast.maketext (testlisteneractivity.this, "xxx", toast.length_ Short)
. Show ();
}


3, the activity itself as a listener, you can directly define the event handler method, although the form is concise, but easy to cause the program confusion

I hope this article will help you with the Android program.

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.