Four ways to do Android Click events (click button)

Source: Internet
Author: User

In the study of Android development and testing, found that different people for the Click event is not the same way, the internet looked up, found that there are four kinds of writing, so want to compare the four different methods

The first method: Anonymous inner class

Code:

Package com.zdx.testdemo1;

Import javax.security.auth.PrivateCredentialPermission;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
Import Android.view.View.OnClickListener;
public class Mainactivity extends Activity {
Private Button btNum1;
Private Toast notify;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BTNUM1 = (Button) Findviewbyid (R.id.bt_click);
Btnum1.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
notify = Toast.maketext (Mainactivity.this, "Click event Call succeeded", Toast.length_short);
Notify.show ();
}
});
}
}

Note here: Notify = Toast.maketext (Mainactivity.this, "Click event Call succeeded", Toast.length_short);

Note here that the mainactivity.this is to be passed instead of this, because it is an anonymous inner class, and the current object is the context of the activity required by the Onclicklistener,maketext method, that is, activity.this, why not Getapplica Tioncontext (), this is because Getapplicationcontext (), returns the context of the application, and the life cycle is the entire application. Activity.this returns the context of the current activity, which is only the activity in which it resides. All we need to do is pass activity.this.

Second approach: Define a class that listens for events

Package com.zdx.testdemo1;

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

public class Mainactivity extends Activity {
Private Button btNum1;
Static Toast notify;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
btnum1= (Button) Findviewbyid (R.id.bt_click);
Btnum1.setonclicklistener (New Clickeventlistener ());
}

Class Clickeventlistener implements onclicklistener{

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.bt_click:
notify = Toast.maketext (Mainactivity.this, "Click event Call succeeded", Toast.length_short);
Notify.show ();
Break
Default
Break
}
}

}
}

Third method: Do not create internal classes, direct activity implementation Onclicklistener interface

Package com.zdx.testdemo1;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
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 btNum1;
Static Toast notify;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
btnum1= (Button) Findviewbyid (R.id.bt_click);
Btnum1.setonclicklistener (this);
}


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.bt_click:
notify = Toast.maketext (Mainactivity.this, "Click event Call succeeded", Toast.length_short);
Notify.show ();
Break
Default
Break
}
}


}

The fourth method: I find the most convenient one, the OnClick property of the button in the XML file definition

Package com.zdx.testdemo1;

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

public class Mainactivity extends Activity {
Static Toast notify;

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


public void Clickevent (View v) {
notify = Toast.maketext (Mainactivity.this, "Click event Call succeeded", Toast.length_short);
Notify.show ();

}
}

Note the name of the OnClick property defined by the XML file must be the same as the name of the Click event method in the activity:

android:onclick= "Clickevent"

public void Clickevent (View v)

I think the third and fourth kind of better, especially the fourth is very convenient, but a lot of development is customary with the first and the second kind of

Four ways to do Android Click events (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.