Android Development-a variety of ways to monitor button click events _android

Source: Internet
Author: User
Tags anonymous xml attribute

Under Android, events occur under the listener, and the Android system responds to keystroke events and touch-screen events, and this article mainly introduces the button click event Method.

One, the realization button clicks the event the method

There are a number of ways to implement the button-click event, which summarizes four commonly used methods:

1. Anonymous inner class

2. External class (Independent Class)

3. Implement Onclicklistener Interface

4. Adding XML attributes

Each of these methods has its advantages and disadvantages, then the following is a detailed explanation of the four implementation methods

Second, the concrete realization

1. Anonymous inner class:

We often see the use of anonymous internal classes in Android development, so we can also use anonymous inner classes when implementing button-click events.

The benefits of this use are:

1 do not need to write a class, directly in the new time to achieve the method to achieve, very convenient.

2 use anonymous inner class when this method is not used anywhere else

3 high cohesion, high cohesion is one of the principles of design, one of the characteristics of anonymous internal class is to have high cohesion.

But there are also deficiencies:

1 when the same method is needed elsewhere, write an anonymous inner class again in that place, making the code highly redundant.

2) inconvenient Late maintenance

A, add a button

 <button
    android:id= "@+id/button1"
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:layout_alignparentleft=" true "
    android:layout_below=" @+id/textview1 "
    android: layout_margintop= "20DP"
    android:text= "method One: Anonymous inner class"/>

B, back to the mainactivity implementation

public class Mainactivity extends activity{
  
  private Button btn1;
  
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
     * * Method One: Use anonymous inner class to implement button button
     's
    //binding button
    Btn1 = (button) Findviewbyid (r.id.button1);
    
    Monitor button Event
    Btn1.setonclicklistener (new Onclicklistener () {
      @Override public
      void OnClick (View v) {
        Toast tot = Toast.maketext (
            mainactivity.this,
            "Anonymous inner Class implementation button Click event",
            Toast.length_long);
        Tot.show ();}}
    );
  }

C, the results of the operation

2, independent class (external Class):

Re-write a separate class to implement the business logic or desired effect

The benefits of this writing are:

1) Under certain circumstances can facilitate maintenance

2 can reduce the redundancy of the code, can be used at the same time to many places

The disadvantages are:

1 when the use of only one time waste of resources, the performance of the program is not high

2 when there are many methods, the code is not readable, at this time is not convenient maintenance

A, add a button

<button
    android:id= "@+id/button2"
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:layout_alignright=" @+id/button1 "
    android:layout_below=" @+id/button1 "
    Android : layout_margintop= "20DP"
    android:text= "Method two: Independent class"/>

B, back to mainactivity implementation, the external class needs to implement the Onclicklistener interface, and rewrite the method

public class Mainactivity extends activity {
  
  private Button btn2;

  
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
     * * Method Two: Independent class implementation Button implementation
     */
    BTN2 = (Button) Findviewbyid (r.id.button2);
    Btn2.setonclicklistener (New Btn2click (this));
    
    
  }


public class Btn2click implements Onclicklistener {

  private context context;
  
  Overloaded Btn2click method Public
  btn2click (context ct) {
    this.context=ct;
  }
  
  @Override public
  void OnClick (View v) {
    Toast tot = toast.maketext (context
        ,
        "Standalone Class implementation button click event",
        Toast.length_long);
    Tot.show ();  
  }


C, Operation effect

3. Implement Onclicklistener Interface:

The principle of the implementation of the Independent class is the same, the advantages and disadvantages are the same size, the implementation of the Onclicklistener interface when the implementation of its OnClick method

A, add button buttons

<button
    android:id= "@+id/button3"
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:layout_alignright=" @+id/button2 "
    android:layout_below=" @+id/button2
    " android:layout_margintop= "20DP"
    android:text= "method three: Implement Interface"/>

B, back to the mainactivity implementation

public class Mainactivity extends activity implements Onclicklistener {

  private Button btn3;
  
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
     * * Method Three: Implement Onclicklistener interface
     * *
    btn3 = (Button) Findviewbyid (r.id.button3);
    Btn3.setonclicklistener (this);
    
  }

  Implement the method in the Onclicklistener interface
  @Override public
  void OnClick (View v) {
    Toast tot = Toast.maketext (
        Mainactivity.this,
        "Interface onclicklistener implement button click event",
        Toast.length_long);
    Tot.show ();
    
  }


C, Operation effect

4. Add XML attributes:

We can add an onclick attribute to the XML to monitor the Click event

The advantage is that it is more convenient and the amount of code can be reduced

But the lack of place is: every time maintenance to the XML inside to change the source code, is not very difficult to maintain it?

A, add a button and add the onclick button

<button
    android:id= "@+id/button4"
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:layout_alignleft=" @+id/button3 "
    android:layout_below=" @+id/button3 "
    android: layout_margintop= "20DP"
    android:onclick= "Btn4click"
    android:text= "method four: Add XML attribute"/>

B, back to mainactivity to implement the onclick button

/
   * Method Four: Add XML attribute/public
  void Btn4click (View v) {
    Toast tot = Toast.maketext (
        Mainactivity.this,
        "Add XML tag to implement button click event",
        Toast.length_long);
    Tot.show ();
  }

C, the results of the operation:

Third, Summary:

1, in the realization of the listening time is required two steps:

1) Bind button buttons

2) Monitoring Button events

1, the specific use of what method to achieve button click event monitoring to see the specific needs, each has its own advantages and disadvantages. The method of using an inner class is recommended if it is used only once, and the method of using the outer class is used more than once; the method of implementing the interface can be implemented in the original class, but the method of adding attributes in XML is not recommended, after all, it is difficult to maintain.

2, the use of internal classes in the development of Android is often used, so very important. The use of internal classes in Java is detailed: http://www.jb51.net/article/36125.htm

3, Android Development has a lot of buttons, but the method of listening is commonly used in these kinds, so can extrapolate, which is why write this reason

4, Toast is an implementation of the effect of Android, is not often see this effect? In the written listening time also toast together to learn, kill the birds with the same ha!!

PS: Seemingly fragmented knowledge points, but all the knowledge points are a line, any industry knowledge points are so, like the first number to appear in arithmetic, first appeared in English letters have the same words ...

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.

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.