Four Click response methods for a Button: Aggregation _ HTML/Xhtml _ webpage Creation

Source: Internet
Author: User
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.

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.