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