Four kinds of onclick response methods for Android learning-button

Source: Internet
Author: User

In Android or Java development, there are four ways to implement the button's Onclicklistener, and you can choose the appropriate usage as needed.

Method One:

Fit Scene: Universal

Button Bt_demo = (button) Findviewbyid (R.id.bt_demo);  Bt_demo.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {///Response clicked Event//... } });

Method Two:

Fit for scene: used when multiple button or button arrays are appropriate

Button Bt_demo = (button) Findviewbyid (R.id.bt_demo);  Bt_demo.setonclicklistener (listener); Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View arg0) {//TODO Auto-gen     erated method Stub switch (Arg0.getid ()) {case R.id.bt_demo://Response clicked Event//... break;     Default:break; }  } }

Method Three:

Fit scenario: Used when multiple button or button arrays are appropriate, which is not very different from method two

Button Bt_demo = (button) Findviewbyid (R.id.bt_demo); Bt_demo.setonclicklistener (New Buttonlistener ());    Private class Buttonlistener implements onclicklistener{@Override public void OnClick (View arg0) {//Response clicked Event   //......  } }

Method Four:

Fit scenario: Suitable for different types of controls on the interface, which implements the OnClick method of all controls in a single method, and looks relatively concise

  //directly Onclicklistener interface in activity:     import  android.view.view.onclicklistener;   public class myactivity extends  activity implements onclicklistener {       @Override  public  void oncreate (bundle savedinstancestate)  {     super.oncreate ( Savedinstancestate);      setcontentview (R.layout.main);      Push button      Button bt_Demo =  Findviewbyid (R.id.bt_demo);      bt_demo.setonclicklistener (This);    }       //Response Click event    public void onclick (VIEW V)  {        switch  (V.getid ())  {           case r.id.bt_demo:      //responding to clicked Events      //......            break;          default:            break;       }     }     }

Reference: http://blog.csdn.net/xcl168/article/details/14452807

This article is from the "alan_y (upspringing)" blog, so be sure to keep this source http://alany.blog.51cto.com/6125308/1636489

Four kinds of onclick response methods for Android learning-button

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.