A summary of several methods to realize the function of Android click button _android

Source: Internet
Author: User
Tags stub

The button control in Android should count as a simpler control, however, it's used very high, and today I've summed up three common ways to achieve its function by clicking button.

1. Most of the time, when we use the button control, it is often "one-time" use, at this time, for the sake of convenience, we generally use the anonymous inner class method, like this:

Copy Code code as follows:

Button1.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
System.out.println ("You clicked on Button1");
}
});

As we can see, such code is not only short, but also clear and understandable, but this method generally applies only if the button is not used more often or "one-time" use

2. when the button has more than one or the use of a lot of button, we need to use the binding listener, in fact, the binding listener there are several methods, but I do not enumerate here, after all, those methods in the actual application is also not common.

Our general approach is to implement the Onclicklistener interface and implement one of the methods, just like this:

Copy Code code as follows:

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button2:
System.out.println ("You clicked on Button2");
Break

Default
Break
}
}


Note: The OnClick method is a method in the Onclicklisten interface, and we must implement it by implementing this interface.

3. This is one of the easiest ways to do what we need to do is add a method and add a property to the button:

Copy Code code as follows:

<button
Android:id= "@+id/button3"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Button3 test"
android:onclick= "ClickHandler"
/>

Among them, we added the onclick attribute more than usual.

So, we need to add the methods we declare in our code:

Copy Code code as follows:

public void ClickHandler (view view) {
System.out.println ("You clicked on Button3");
}

Finally, paste out the complete source code and the realization effect screenshot:

1. layout file

Copy Code code as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity "
android:orientation= "Vertical"
>

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<button
Android:id= "@+id/button1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Button1 test"
/>
<button
Android:id= "@+id/button2"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Button2 test"
/>
<button
Android:id= "@+id/button3"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Button3 test"
android:onclick= "ClickHandler"
/>

</LinearLayout>


Effects in the form of:


2. Test source code
Copy Code code as follows:

Package com.example.buttonclicktest;

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;

public class Mainactivity extends activity implements onclicklistener{

Private Button button1 = null;
Private Button button2 = null;

 public void Findbutton () {
  button1 = (Button) Findviewbyid (r.id.button1);
  button2 = (Button) Findviewbyid (R.id.button2);
 }

  @Override
 protected void OnCreate (Bundle savedinstancestate) {
   Super.oncreate (savedinstancestate);
  setcontentview (R.layout.activity_main);

  findbutton ();
  button2.setonclicklistener (this);

  button1.setonclicklistener (New Onclicklistener () {

    @Override
   public void OnClick (View v) {
    //TODO auto-generated method Stub
    system.out.println ("You clicked on Button1");
   }
  });
 }

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button2:
System.out.println ("You clicked on Button2");
Break

Default
Break
}
}

public void ClickHandler (view view) {
System.out.println ("You clicked on Button3");
}

}


When we click on the button, we can see in the logcat that the result is as follows:



We can see from the result that, three kinds of methods can realize the function of button clicks, we can choose the corresponding method according to the different situation.

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.