从 android 开发的角度来看,当您编程时,您更喜欢为监听器实现哪种方式?或者您认为哪种方式最适合可读代码?我举了两个关于这些事情的例子,但考虑更复杂的类,比如有多个 Listener 的类:)
第一个例子是匿名类:
public class SenderReceiverActivity extends Activity {
Button cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending);
cancelButton = (Button) findViewById(R.id.button1);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}}
第二个实现接口的例子:
public class SenderReceiverActivity extends Activity implements OnClickListener {
Button cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending);
cancelButton = (Button) findViewById(R.id.button1);
cancelButton.setOnClickListener(this);
}
public void onClick(View v) {
}
}