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
MySQL Users and Privileges In MySQL, there is a d...
Centos7 startup process: 1.post(Power-On-Self-Tes...
What is MIME TYPE? 1. First, we need to understan...
Table of contents 1. Introduction 2. Main text 2....
1. The ul tag has a padding value by default in M...
1. Common MySQL configuration All the following c...
For example, when you create a new table or updat...
When installing in MySQL 8.0.16, some errors may ...
1. Inline reference: used directly on the label, ...
1. Setting case sensitivity of fields in the tabl...
For a website, usability refers to whether users c...
<base target=_blank> changes the target fram...
Preface The best method may not be the one you ca...
Since enabling https access for the entire site, ...
This article introduces the development environme...