4 ways to read the Android day01-android button click event

Source: Internet
Author: User

button click event, plainly on a method, Setonclicklistener (Onclicklistener).

As long as the passed parameter is a subclass of the Onclicklistener interface, it is easy to think of 2 implementations that are anonymous inner classes and define a class to implement this interface . There are also 2 ways to achieve this, and it is important to focus on:

1. Let the activity itself implement the Onclicklistener interface , passing the Setonclicklistener parameter with this.

Then, in the onclick (View V) method, the event source ID is judged.

Switch (V.getid ())

{

Case r.id.xxx000

Response code

Break

Case r.id.xxx111

Response code

Break

}

This is the way to define a class to implement an interface, just so that we don't define a class more. Directly let activity itself implement Onclicklistener interface, implement the OnClick method inside it.

This is defined in cases where multiple click events are available.

2. Define a Label attribute android:onclick= "method name" in the layout file, and then define a method in the. java file that the Android project calls this layout, the public method name (View v), Remember to public and have view parameters.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/B8/wKioL1WmeISQsg6SAAG2k_kZ2I0908.jpg "title=" click. PNG "alt=" Wkiol1wmeisqsg6saag2k_kz2i0908.jpg "/>

By looking at the source of the button's parent view, you can see that the onclick bottom uses the reflection principle, but does not use violent reflection, so define the method with public.

This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1675142

4 ways to read the Android day01-android button click event

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.