Four ways to click on the Android button

Source: Internet
Author: User
Tags switch case

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " >1) Click the Implementation class interface of the button; Disadvantages, more buttons, code redundancy;</span>
1.findViewByID ();
2.btn.setonclicklistener (New Myonclicklistener ());//interface for implementing classes;
3.class Myonclicklistener implements onclicklistener{
public void OnClick (View v) {
Toast.maketext (mainactivity, "Print ...", 0). Show ();
}
}

2), the method of implementing the class interface in the class, using the current pass this to implement the OnClick () method, and add a switch case inside to determine the button to operate;


Very many buttonsPublic class Mainactivity extends actionbaractivity implements Onclicklistener



1.findViewByID ();//Get ID
2.btn.setonclicklistener (this);//Set button listener;
3.public void OnClick (View v) {
Switch (V.getid ()) {
Case R.ID.BTN1:
Toast.maketext (This, "Print ...", 0). Show ();
}
}


3) Under the button listener directly new an anonymous inner class//This anonymous inner class can be an interface
Btn.setonclicklistener (New onclicklistener{
Public OnClick (View v) {
Toast.maketext (Mainactivity.this, "string ....", 0). Show ();
}
});


4) No need to define the ID of the button at this time only for demo example
1. Using the XML layout file
android:onclick= "haha"
2. Automatically generate a "haha" method in the Java implementation logic method
public void haha (View v) {
Toast.maketext (mainactivity.this, "input ....", 0). Show ();

}


Attach source code Mainactivity.java in Low:

Package Com.weibo.onclick;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.ActionBar ; Import Android.support.v4.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.viewgroup;import android.widget.button;import android.widget.toast;import android.os.Build; public class Mainactivity extends Actionbaractivity implements Onclicklistener {@Override protected void onCreate (B        Undle savedinstancestate) {super.oncreate (savedinstancestate);                        Setcontentview (R.layout.fragment_main);        Button Btn_click1 = (button) Findviewbyid (R.ID.BTN_CLICK1);        Button Btn_click2 = (button) Findviewbyid (R.ID.BTN_CLICK2);        Button Btn_click3 = (button) Findviewbyid (R.ID.BTN_CLICK3);                Button Btn_click4 = (button) Findviewbyid (R.ID.BTN_CLICK4); /*//1. Inner class method Get button        Btn_click1.setonclicklistener (New Myoncliklistener ());        Btn_click2.setonclicklistener (New MyOnClikListener2 ());      Btn_click3.setonclicklistener (New MyOnClikListener3 ()); */*//2. Anonymous inner class implementation Btn_click1.setonclicklistener (new Onclicklistener () {@Overridepublic void O                Nclick (View v) {toast.maketext (Mainactivity.this, button button 1, 0). Show ();}); Btn_click2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Toast.maketext ( Mainactivity.this, "button button 2", 0). Show ();}});        */*//3. Use the switch method to implement Btn_click1.setonclicklistener (this);        Btn_click2.setonclicklistener (this);        Btn_click3.setonclicklistener (this); Btn_click4.setonclicklistener (this); */} Class Myoncliklistener implements onclicklistener{@Overridepubl        IC void OnClick (View v) {toast.maketext (Mainactivity.this, "press button 1", 0). Show (); } class MyOnClikListener2 implements onclicklistener{@Overridepublic void OnClick (View v) {toast.maketext (Mainactivity.this, "press button 2", 0). Show (); } class MyOnClikListener3 implements onclicklistener{@Overridepublic void OnClick (View v) {Toast.maketext (mainactiv        Ity.this, "press button 3", 0). Show (); } @Overridepublic void OnClick (View v) {switch (V.getid ()) {R.id.btn_click1:toast.maketext (this, "BT1", 0). Show (); Break;case R.id.btn_click2:toast.maketext (This, "BT2", 0). Show (); Break;case R.id.btn_click3:toast.maketext (This, " BT3 ", 0). Show (); Break;default:toast.maketext (This," Bt4 ", 0). Show ();}} public void haha (View v) {Toast.maketext (this, "haha", 0). Show ();   public void Point (View v) {Toast.maketext (this, "dot", 0). Show (); }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Four ways to click on the Android button

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.