Three methods of onclick event writing in Activity

Source: Internet
Author: User

The functions and effects of the three methods are the same.

Java files

View plaincopy to clipboardprint?
Package com. wyw. Demo;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;


Public class activity extends activity {
/** Called when the activity is first created .*/
Edittext EV1;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

EV1 = (edittext) findviewbyid (R. Id. edittext01 );
// Method 1
Button btn1 = (button) findviewbyid (R. Id. button01); // get button Resources
Btn1.setonclicklistener (New button. onclicklistener () {// create a listener
@ Override
Public void onclick (view v ){
String strtmp = "Click button01 ";
Ev1.settext (strtmp );
}
});

// Method 2
Button btn2 = (button) findviewbyid (R. Id. button02); // get button Resources
Btn2.setonclicklistener (listener); // sets the listener
}

Button. onclicklistener listener = new button. onclicklistener () {// create a listener object
@ Override
Public void onclick (view v ){
String strtmp = "Click button02 ";
Ev1.settext (strtmp );
}
};

// Method 3 (available in Android 1.6 and later versions)
Public void btn3onclick (view ){
String strtmp = "Click button03 ";
Ev1.settext (strtmp );
}
}
Package com. wyw. Demo;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;

Public class activity extends activity {
/** Called when the activity is first created .*/
Edittext EV1;
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

EV1 = (edittext) findviewbyid (R. Id. edittext01 );
// Method 1
Button btn1 = (button) findviewbyid (R. Id. button01); // get button Resources
Btn1.setonclicklistener (New button. onclicklistener () {// create a listener
@ Override
Public void onclick (view v ){
String strtmp = "Click button01 ";
Ev1.settext (strtmp );
}
});

// Method 2
Button btn2 = (button) findviewbyid (R. Id. button02); // get button Resources
Btn2.setonclicklistener (listener); // sets the listener
}

Button. onclicklistener listener = new button. onclicklistener () {// create a listener object
@ Override
Public void onclick (view v ){
String strtmp = "Click button02 ";
Ev1.settext (strtmp );
}
};

// Method 3 (available in Android 1.6 and later versions)
Public void btn3onclick (view ){
String strtmp = "Click button03 ";
Ev1.settext (strtmp );
}
}

XML file

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button Android: text = "button01"
Android: Id = "@ + ID/button01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Button>

<Button Android: text = "button02"
Android: Id = "@ + ID/button02"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Button>

<Button Android: text = "button03"
Android: Id = "@ + ID/button03"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onclick = "btn3onclick">
</Button>

<Edittext Android: text = "@ + ID/edittext01"
Android: Id = "@ + ID/edittext01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Edittext>
</Linearlayout>

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wyw1213/archive/2011/03/27/6282277.aspx

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.