Overview of four event monitoring methods for Android Click button

Source: Internet
Author: User

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

123456789101112131415 <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 Class Mainactivity extends Activity {    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 (New Onclicklistener () {public            void OnClick (View arg0) {                Toast.maketext ( Mainactivity.this, "button clicked", Toast.length_long). Show ();}}        );    }}

2. Internal classes as listeners

1234567891011121314151617181920212223242526272829 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;publicclassMainActivity extends Activity {    private Button btnButton;    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnButton=(Button)findViewById(R.id.button1);        MyClicked myClicked=newMyClicked();        btnButton.setOnClickListener(myClicked);    }    privateclassMyClicked implements OnClickListener{         @Override        publicvoidonClick(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:

12345678910111213141516 <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:

123456789101112131415161718 package com.basillee.blogdemo;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.widget.Toast;publicclassMainActivity extends Activity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    publicvoidmyClick(View v){        Toast.makeText(getApplicationContext(),"Button clicked!", Toast.LENGTH_LONG).show();    }}
transferred from: http://www.cnblogs.com/BasilLee/p/4101284.html

Overview of four event monitoring methods for Android Click 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.