Summary of Button's four Click response methods

Summary of Button's four Click response methods

Button is used quite a lot. Here I have sorted out its event handling methods and found that there are many ways to implement it. I prefer the second one. What about you? Which one do you use most?

Implementation 1:


Copy code
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)
{
//Respond to Clicked event
//......
}
});

Implementation 2:


Copy code
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:
//Respond to Clicked event
//......
break;
default:
break;
}
}
}

Implementation three:


Copy code
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) {
//Respond to Clicked event
//......
}
}

Implementation Four:


Copy code
The code is as follows:

//Directly use the 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);
}
//Respond to Click event
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_Demo:
//Respond to Clicked event
//......
break;
default:
break;
}
}
}

Thank you for such a comprehensive summary. Although I know all of this, I lack the ability to summarize it myself.

<<:  Summary of accurate calculations of various distances/scroll distances in a window

>>:  Sample code for implementing radar chart with vue+antv

Recommend

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...

Centos7 startup process and Nginx startup configuration in Systemd

Centos7 startup process: 1.post(Power-On-Self-Tes...

The Complete List of MIME Types

What is MIME TYPE? 1. First, we need to understan...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

25 div+css programming tips and tricks

1. The ul tag has a padding value by default in M...

MySQL series 15 MySQL common configuration and performance stress test

1. Common MySQL configuration All the following c...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

Summary of four ways to introduce CSS (sharing)

1. Inline reference: used directly on the label, ...

How to operate MySql database with gorm

1. Setting case sensitivity of fields in the tabl...

25 Tools to Improve Website Usability and Conversion Rates

For a website, usability refers to whether users c...

base target="" controls the link's target open frame

<base target=_blank> changes the target fram...

Detailed explanation of Linux one-line command to process batch files

Preface The best method may not be the one you ca...

Solution for using Baidu share on Https page

Since enabling https access for the entire site, ...