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

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

Detailed explanation of MySQL remote connection permission

1. Log in to MySQL database mysql -u root -p View...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

Some notes on modifying the innodb_data_file_path parameter of MySQL

Preface innodb_data_file_path is used to specify ...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

Prometheus monitors MySQL using grafana display

Table of contents Prometheus monitors MySQL throu...

CentOS 7.x docker uses overlay2 storage method

Edit /etc/docker/daemon.json and add the followin...

Four ways to switch tab pages in VUE

Table of contents 1. Static implementation method...

Six weird and useful things about JavaScript

Table of contents 1. Deconstruction Tips 2. Digit...

Win10 installation Linux system tutorial diagram

To install a virtual machine on a Windows system,...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...