Four ways to monitor event implementations

Source: Internet
Author: User

1. Initialize the control you currently need, how to initialize a control ... private Button xxxx
Findviewbyid---Return the object of a view ...... Need to strongly turn into its subclass button object
Findviewbyid---is how to find the ID of the view ... Through the ID in R
2. Set the listener of the button to implement what we need to do to click on the button via the listener

Way One:

Anonymous inner classes implement event snooping:
New A Onclicklistener class in the Setonclicklistener method of the button, and write the code that needs to be implemented in the implementation of the Onclicklistener class.
Such as:
Button Loninbutton = (button) findviewid (R.ID.BT1);
Loginbutton.setonclicklistener (New Onclicklistener () {implemented content});

Way two:
External Class Listener events:
There are multiple buttons, each with an identical action, and each button has its own action, which uses an external class to listen for events.
Add a Myonclicklistener class that implements the Onclicklistener interface
Rewrite the onclick () method to add actions that need to be implemented in the method body;
Then implement the individual actions under each interface, such as:
Bt2.setonclicklistener (New Myonclicklistener () {
public void OnClick (View v) {
Super.onclick (v);//Implement the OnClick () of the parent class; action
Toast.maketext (Mainactivity.this, "BT3 logic to execute", Toast.length_short). Show ();
} });

Way three:
Interface mode for event monitoring:

The Activitymain class implements the Onclicklistener interface and then overrides the OnClick () of the Onclicklistener interface;
At the time of invocation, write this directly, such as:
Imgbt.setonclicklistener (this);

Mode four:

Independent events
Loginbutton.setonclicklistener (listener);
Externally separate:
Onclicklistener listener = new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
System.out.println ("Class of the standalone implementation triggered");
}
};

Four ways to monitor event implementations

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.