Four ways to write Android notes---Click events

Source: Internet
Author: User

four ways to read Android Click events:

1. Implement the Onclicklistener interface in the form of an internal class. Defining Click Events

Class mainactivity extents activity{//... private class MyListener implements Onclicklistener{public void Onclick (View v) {// ... Click the response of the event}}}


2. Using Anonymous internal classes
?? Disadvantage: Too many anonymous internal classes are required if there are too many click events
Class mainactivity extents activity{protected void onCreate (...) {//The Bt_call.setonclicklistener (new onclicklistener{public void Onclick (View v) {//...) is implemented as an anonymous inner class. Click the response of the event}});}}


3. The mainactivity implementation of the Onclicklistener interface, the actual development of a very frequent use of the wording
?? Strengths: Able to solve the problem of Method 2
Class mainactivity extents Activity implements onclicklistener{protected void OnCreate (...) {// ...} /*** Click event */public void OnClick (View v) {//based on different Id to determine what action switch (V.getid ()) {case r.id.bt_call:callphone ();//define function, Make a call to break;

4. The method of binding a click event in the configuration file is not used frequently
Activity_main.xml:
<button android:onclick= "Callphone"//Bind the button's Click event. Will proactively find the method in the Activity .../>

Mainactivity.java:
Class mainactivity extents Activity {/*** Click event. In this case the method must take the parameter view v*/public void Callphone (view v) {//...}}

Android Notes---Four ways to click events

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.