Four common ways to summarize the Android button click event _android

Source: Internet
Author: User
Tags anonymous event listener stub

A lot of people who learn the Android program will find that everyone has different preferences for how the code is written, and it's more obvious how the control responds to events differently. Therefore, this article summed up the writing, comparing the advantages and disadvantages of various methods, I hope that the flexibility to choose the encoding method can have a certain reference value.

The XML file code is as follows:

<button
    android:id= "@+id/button1"
    android:layout_width= "Wrap_content"
    Wrap_content "
    android:text=" Button1 "/>

  <button
    android:id="
    @+id/button2 "Android:layout_ Width= "Wrap_content"
    android:layout_height= "wrap_content" android:text= "Button2"
    />

The four methods are described as follows:

Anonymous inner class:

public class Testbuttonactivity extends activity {

  Button btn1, btn2;
  Toast TST;

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_test_button);

    BTN1 = (Button) Findviewbyid (r.id.button1);
    BTN2 = (Button) Findviewbyid (r.id.button2);

    Btn1.setonclicklistener (New Onclicklistener () {

      @Override public
      void OnClick (View v) {
        //TODO auto-generated method Stub
        Toast TST = Toast.maketext (Testbuttonactivity.this, "111111111", Toast.length_short);
        tst.show ();

      }
    );

    Btn2.setonclicklistener (New Onclicklistener () {

      @Override public
      void OnClick (View v) {
        //TODO auto-generated method Stub
        Toast TST = Toast.maketext (Testbuttonactivity.this, "222222222", Toast.length_short);
        tst.show ();}}
    );
}

Custom Click event Listener class:

public class Testbuttonactivity extends activity {Button btn1, btn2;

  Toast TST; Class Myclicklistener implements Onclicklistener {@Override public void OnClick (View v) {//TODO Auto-gen erated method Stub switch (V.getid ()) {Case r.id.button1:tst = Toast.maketext (testbuttonactivity.thi
        S, "111111111", Toast.length_short);
        Tst.show ();
      Break
        Case r.id.button2:tst = Toast.maketext (Testbuttonactivity.this, "222222222", Toast.length_short);
        Tst.show ();
      Break
      Default:break;
    }} @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_test_button);
    BTN1 = (Button) Findviewbyid (R.id.button1);

    BTN2 = (Button) Findviewbyid (R.id.button2);
    Btn1.setonclicklistener (New Myclicklistener ());
  Btn2.setonclicklistener (New Myclicklistener ());

 }
}

The activity inherits View.onclicklistener, the activity implements the OnClick (view view) method, and the OnClick (view view) method to handle the button with different IDs represented by Switch-case

public class Testbuttonactivity extends activity implements Onclicklistener {

  Button btn1, btn2;
  Toast TST;

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_test_button);

    BTN1 = (Button) Findviewbyid (r.id.button1);
    BTN2 = (Button) Findviewbyid (r.id.button2);

    Btn1.setonclicklistener (this);
    Btn2.setonclicklistener (this);
  }

  @Override public
  void OnClick (View v) {
    //TODO auto-generated a stub
    switch (V.getid ()) {
    case R.id.button1:
      TST = Toast.maketext (This, "111111111", toast.length_short);
      Tst.show ();
      break;
    Case R.id.button2:
      TST = Toast.maketext (This, "222222222", toast.length_short);
      Tst.show ();
      break;
    Default: Break;}}}


The last one is the way I see it today, in an XML file "displays the OnClick property of the specified button, so that when you click the button, it invokes the Click () method in the corresponding activity using reflection."

<button
    android:id= "@+id/button1"
    android:layout_width= "Wrap_content"
    Wrap_content "
    android:onclick=" OnClick "
    android:text=" Button1/> <button

  android:id=
    Id/button2 "
    android:layout_width=" wrap_content "
    android:layout_height=" wrap_content "
    android:o" nclick= "OnClick"
    android:text= "Button2"/>

Here in the end of the game Android: Press alt+/will have the onclick properties of the prompt, but input to android:onclick= "" Place pressed alt+/did not prompt the onclick option, let me suddenly feel here seems to have a problem.

public class Testbuttonactivity extends activity {

  Button btn1, btn2;
  Toast TST;

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_test_button);
  }

  Note that there is no @Override tag public
  void OnClick (View v) {
    //TODO auto-generated Method stub
    switch (V.getid ()) {Case
    r.id.button1:
      TST = Toast.maketext (This, "111111111", toast.length_short);
      Tst.show ();
      break;
    Case R.id.button2:
      TST = Toast.maketext (This, "222222222", toast.length_short);
      Tst.show ();
      break;
    Default: Break;}}}


This kind of writing does not have to declare a button in the entire code to implement the button's Click event.

These are four ways to implement a button click event.

A rough summary, that is, when the button is less with anonymous internal class will be faster, such as writing a demo test or landing interface.

I still choose the third method, which is convenient for many buttons.

About the fourth method, I feel most convenient, but read a lot of code or feel that the writing is not popular, interested friends can study this. I believe there will be a lot of gains.

I hope this article will help you in your study of Android programming.

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.