Copy codeThe Code is as follows: findViewById (R. id. myButton). setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Do stuff
}
});
The disadvantage of using the above method to add Listener is that if there are too many controls, the number of Listener will increase. Therefore, you can use the following tips to reduce the number of Listener:
Copy codeThe Code is as follows: View. OnClickListener handler = View. OnClickListener (){
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. Button01: // doStuff
Break;
Case R. id. Button02: // doStuff
Break;
}
}
}
FindViewById (R. id. myButton). setOnClickListener (handler );
FindViewById (R. id. mytherbutton). setOnClickListener (handler );
In Android1.6, adding Listener is quite simple (it feels more like programming on Web pages !), The procedure is as follows:
1. Define the Button in layout and specify the Listener of the response.
Copy codeThe Code is as follows: <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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: text = "Button01"
Android: id = "@ + id/Button01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "myClickHandler01"
/>
<Button
Android: text = "Button02"
Android: id = "@ + id/Button02"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "myClickHandler02"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</LinearLayout>
The following two lines are new features:
Android: onClick = "myClickHandler01"
Android: onClick = "myClickHandler02"
2. Define the public methods myClickHandler01 and myClickHandler02 in the activity (note that the two methods must have a View parameter ).
Copy codeThe Code is as follows: package com. ray. test;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Public class TestOnClickListener extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void myClickHandler01 (View target ){
SetTitle ("myClickHandler01 ");
}
Public void myClickHandler02 (View target ){
SetTitle ("myClickHandler02 ");
}
}
Of course, you can also use the following method:
Set the two buttons to the same Listener.
Android: onClick = "myClickHandler"
Android: onClick = "myClickHandler"
Copy codeThe Code is as follows: package com. ray. test;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Public class TestOnClickListener extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void myClickHandler (View target ){
Switch (target. getId ()){
Case R. id. Button01:
SetTitle ("myClickHandler01 ");
Break;
Case R. id. Button02:
SetTitle ("myClickHandler02 ");
Break;
}
}
}