Event Listener for android--button

Source: Internet
Author: User

Summary of four kinds of event monitoring methods for button buttons

First, we'll define a button space in the Activity_main.xml.

<relativelayout 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 ">    <button        android:id=" @+id/button1 "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:layout_alignparentleft= "true"        android:layout_ Alignparenttop= "true"        android:text= "button"/></relativelayout>

Okay, now we're going to look at four ways of clicking the event:

1. The most common way of writing--Anonymous inner class as event listener class

Package Com.basillee.blogdemo;import android.os.bundle;import android.app.activity;import Android.view.View; Import Android.view.view.onclicklistener;import android.widget.button;import android.widget.Toast; Public classMainactivity extends Activity {PrivateButton Btnbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Btnbutton=(Button) Findviewbyid (R.id.button1); Btnbutton.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {toast.maketext (mainactivity. This,"Button Clicked", Toast.length_long). Show ();    }        }); }}

2. Internal classes as listeners

Package Com.basillee.blogdemo;import Android.os.bundle;import Android.app.activity;import android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public Class Mainactivity extends Activity {private Button Btnbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); btnbutton= (Button) findViewById ( R.id.button1); myclicked myclicked=new myclicked (); Btnbutton.setonclicklistener (myclicked);} Private class myclicked implements onclicklistener{@Overridepublic void OnClick (View arg0) {Toast.maketext ( Getapplicationcontext (), "button clicked!", Toast.length_long). Show ();}}

3.Activity Direct implementation of Onclicklistener interface

Package Com.basillee.blogdemo;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;

public class Mainactivity extends Activity implements onclicklistener{

Private Button Btnbutton;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
btnbutton= (Button) Findviewbyid (R.id.button1);
Btnbutton.setonclicklistener (this);
}
@Override
public void OnClick (View arg0) {
Toast.maketext (This, "button clicked!", Toast.length_long). Show ();
}

}

4. In the layout file, use the Android:onclick property to specify the corresponding method name implementation

The Activity_main.xml code is as follows:

<relativelayout 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 ">    <button        android:id=" @+id/button1 "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:layout_alignparentleft= "true"        android:layout_ Alignparenttop= "true"        android:text= "button"         android:onclick= "Myclick"/></relativelayout>

The Mainactivity.java code is as follows:

Package Com.basillee.blogdemo;import Android.os.bundle;import Android.app.activity;import android.view.View;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Myclick (View v) {toast.maketext (Getapplicationcontext (), "button clicked!", Toast.length_long). Show ();}}

  

Event Listener for 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.