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:
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:
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:
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:
//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.
Statement: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