Inside the button element, you can place content, such as text or images. This is the difference between this element and the button created using the input element. Today we are talking about four methods of button's click response. We have attached the code and hope it will be helpful to you. Here we have sorted out its event handling methods, there are still many implementation methods. I prefer the second one. Which one is the most commonly used?
Implementation 1:
The Code is as follows:
Button bt_Demo = (Button) findViewById (R. id. bt_Demo );
Bt_Demo.setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// Response to the Clicked event
//......
}
});
Implementation 2:
The Code is as follows:
Button bt_Demo = (Button) findViewById (R. id. bt_Demo );
Bt_Demo.setOnClickListener (listener );
Private OnClickListener listener = new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Switch (arg0.getId ()){
Case R. id. bt_Demo:
// Response to the Clicked event
//......
Break;
Default:
Break;
}
}
}
Implementation 3:
The Code is as follows:
Button bt_Demo = (Button) findViewById (R. id. bt_Demo );
Bt_Demo.setOnClickListener (new ButtonListener ());
Private class ButtonListener implements OnClickListener {
@ Override
Public void onClick (View arg0 ){
// Response to the Clicked event
//......
}
}
Implementation 4:
The Code is as follows:
// OnClickListener interface in Activity:
Import android. view. View. OnClickListener;
Public class MyActivity extends Activity implements OnClickListener {
@ Override public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Button
Button bt_Demo = (Button) findViewById (R. id. bt_Demo );
Bt_Demo.setOnClickListener (this );
}
// Responds to the Click Event
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. bt_Demo:
// Response to the Clicked event
//......
Break;
Default:
Break;
}
}
}
Thanks for the comprehensive summary, although I know all this, I have no summary.