Home > Web Front-end > HTML Tutorial > Summary of Four Click Response Methods of Button_HTML/Xhtml_Web Page Production

Summary of Four Click Response Methods of Button_HTML/Xhtml_Web Page Production

WBOY
Release: 2016-05-16 16:36:51
Original
1683 people have browsed it

Button is used a lot. I have sorted out its event handling methods here and found that there are many implementation methods. I prefer the second one. What about you, which one is the most commonly used?

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)
{
//Response 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:
//Response 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) {
//Response Clicked event
//......
}
}

Implementation Four:


Copy code
The code is as follows:

//Direct 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);

//Response to Click event
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_Demo:
//Respond to Clicked event
/ /......
break;
default:
break;
}
}

Thanks for such a comprehensive summary. Although I know all this, I lack a summary.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template