Explain several ways to implement the Click event in Android _android

Source: Internet
Author: User
Tags anonymous

In the previous posting several times to use the Click event Processing implementation, a friend asked, found a lot of button click Implementation, but there are many blog use of the implementation of the way there are not the same, in the end is how. Today we are going to summarize how the click event is implemented.

The implementation of the Click event is roughly divided into the following three kinds:

(1) The activity implements the interface way realizes the Click event (often uses)

(2) Custom method, using configuration file Android:onclick

(3) using the internal class method to implement

(4) Use Anonymous inner class implementation to introduce the following types of click events to implement the way:

Let's use the code to simply demonstrate how the next few click events are implemented:

(1) The activity implements the interface way realizes the Click event

/**
 * Activity Implementation Interface mode implementation Click event
 * View.onclicklistener Implementation OnClick (view view) {} method
 * in the Oncre Registered event in Ate method/public
class Mainactivity extends Appcompatactivity implements View.onclicklistener {
  Private Button btn;
  protected void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    BTN = (Button) Findviewbyid (R.id.button);
    Btn.setonclicklistener (this);
  }
  public void OnClick (View v) {
    toast.maketext (mainactivity.this, "Implement Interface Mode", Toast.length_long). Show ();
  }

(2) Custom method, using layout configuration file Android:onclick

/**
 * Use configuration file to implement click event
 * Use the OnClick property in the configuration file in layout to specify the method to handle when triggering
 an event, * provide a method with the same name in the activity format as public void XXX ( View v) {...}
 *
/public class Mainactivity extends appcompatactivity {
  protected void onCreate (Bundle savedinstancestate) { C7/>super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
  }
  public void Click (View v) {
    toast.maketext (mainactivity.this, Custom, Toast.length_long). Show ();

(3) using the internal class method to implement

/**
 * Use the internal class method to implement Click event
 * Define a View.onclicklistener implementation class, implement the OnClick method.
 * Registering events in the OnCreate method of the activity/public
class Mainactivity extends Appcompatactivity {
  private Button B TN;
  protected void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    BTN = (Button) Findviewbyid (r.id.button2);
    Btn.setonclicklistener (New Listener ());
  }
  Class Listener implements view.onclicklistener{
    @Override public
    void OnClick (View v) {
      Toast.maketext (Mainactivity.this, "inner class", Toast.length_long). Show ();}}

(4) using anonymous inner classes to implement

/**
 * Use Anonymous inner class method to implement Click event
 * Register button click event directly using anonymous inner class/public
class Mainactivity extends Appcompatactivity {
  private Button btn;
  protected void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    BTN = (Button) Findviewbyid (r.id.button3);
    Btn.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (View v) {
        Toast.maketext (mainactivity.this, "Anonymous inner class", Toast.length_long). Show ();}}

Add:

Different components to implement different types of click events, such as Onitemclicklistener,oncheckedchangelistener,onratingbarchangelistener, Onmultichoiceclicklistener,ondate/timesetlistener,onscrolllistener,onchildclicklistener,setontouchlistener, Onpagechangelistener,onmenuitemclicklistener,oneditoractionlistener,oneditoractionlistener and so on various click event Processing forms.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.