2 types of listener implementations for Android-based click Listeners

Source: Internet
Author: User

Click Listener Interface: Onclicklistener

Abstract function OnClick (View v)

There are 2 methods for monitoring:

1: Current class inheritance Click Listener Interface

Package Com.example.button;

Import android.app.Activity;

Import Android.os.Bundle;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.TextView;

public class Mainactivity extends Activity implements onclicklistener{

Private Button Btn_ok,btn_cancel;

Private TextView TV;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

btn_ok= (Button) Findviewbyid (R.ID.BTN_OK);

Btn_cancel= (Button) Findviewbyid (r.id.btn_cancel);

tv= (TextView) Findviewbyid (r.id.tv);

Btn_ok.setonclicklistener (this);

Btn_cancel.setonclicklistener (this);

}

public void OnClick (View v)

{

if (V==BTN_OK)

{

Tv.settext ("OK button trigger event");

}else if (v==btn_cancel)

{

Tv.settext ("Cancel button trigger event");

}

}


@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}


}

2: Using the inner class to implement the Click Listener interface

Package Com.example.button;

Import android.app.Activity;

Import Android.os.Bundle;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.TextView;


public class Mainactivity extends activity{

Private Button Btn_ok,btn_cancel;

Private TextView TV;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

btn_ok= (Button) Findviewbyid (R.ID.BTN_OK);

Btn_cancel= (Button) Findviewbyid (r.id.btn_cancel);

tv= (TextView) Findviewbyid (r.id.tv);

Btn_ok.setonclicklistener (New Onclicklistener ()

{

public void OnClick (View arg0)

{

Tv.settext ("OK Trigger event");

}

}

);


Btn_cancel.setonclicklistener (New Onclicklistener ()

{

public void OnClick (View arg0)

{

Tv.settext ("Cancel button trigger event");

}

}

);

}



@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}


}


This article is from the "Hagar" blog, make sure to keep this source http://7832308.blog.51cto.com/7822308/1429158

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.