关于在Android中实现监听器的问题
在Android中,是在Activity中实现监听器更好,还是为每个View单独设置监听器更好?
例如:
public class NewActivity extends Activity implements OnClickListener {
public void onClick(View v) {
}
或
btnView.setOnClickListener(new OnClickListener({...});
In Android, is it better to implement Listeners in your Activity, or to individually set Listeners for each View?
For example:
public class NewActivity extends Activity implements OnClickListener {
public void onClick(View v) {
}
or
btnView.setOnClickListener(new OnClickListener({...});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有太大区别。匿名类的优点是任何具有引用的人都无法调用其实现
到你的对象。缺点是你最终会得到一个
一堆额外的课程。您可以选择您认为更适合您需求的选项。
There isn't much of a difference. The advantage to an anonymous class is that the implementation is not callable by anyone with a reference
to your object. The downside is that you end up with a
bunch of extra classes. You can choose which you feel better suits your needs.
没有性能差异,因此请选择使代码更易于阅读的选项
There is no performance difference so choose whatever makes your code easier to read