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
Are you still using rem flexible layout? Does it ...
1. Log in to MySQL database mysql -u root -p View...
1. Docker imports local images Sometimes we copy ...
Preface innodb_data_file_path is used to specify ...
[LeetCode] 176. Second Highest Salary Write a SQL...
When Mysql occupies too much CPU, where should we...
Table of contents Prometheus monitors MySQL throu...
Edit /etc/docker/daemon.json and add the followin...
This time I will talk about the skills of develop...
1. Download Maven Maven official website: http://...
Table of contents 1. Static implementation method...
Table of contents 1. Deconstruction Tips 2. Digit...
To install a virtual machine on a Windows system,...
Table of contents Preface Introduction to Bezier ...
K8s k8s is a cluster. There are multiple Namespac...