Four Technologies for Android button listeners

Source: Internet
Author: User

Android development often use a variety of listeners, Android Listener writing and Java have different places;

Using an inner class to implement an interface in 1,activity, creating an internal class instance using the Add method is similar to Java

Create an instance of the listener

Mylis lis = new Mylis ();

To add a listener to a button using the Add method

Internal class creation Listener

Android Code:
    1. Class Mylis implements onclicklistener{
    2. @Override
    3. public void OnClick (View v) {
    4. }
    5. }

2, anonymous class add listener

Android Code:
    1. Button btn = (button) Findviewbyid (R.ID.BTN);
    2. Btn.setonclicklistener (New Onclicklistener () {
    3. @Override
    4. public void OnClick (View v) {
    5. }
    6. });

3, Implement Interface

Adding the OnClick method to the 4,xml configuration file

Android Code:
    1. Configure the notation in the button XML
    2. android:onclick="Change"
    3. In the activity page, just create the method directly
    4. public void Change (View v) {
    5. }

Four Technologies for Android button listeners

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.