在Android中;岂不是更好施加OnClickListener或使用安卓的onClick? [英] In Android; is it better applying an OnClickListener or use android:onClick?

查看:204
本文介绍了在Android中;岂不是更好施加OnClickListener或使用安卓的onClick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问性能会更好用的onClick?如果我使用的onClick 我不要要设置的机器人:ID 的(也避免了一个新View.OnClickListener 的),这是否提高性能,所有?或者是幕后findViewById出现?

相同的效果

本页面提供了这两种方法的任何benifit的一个选项,但一些指导。

<一个href="http://developer.android.com/reference/android/widget/Button.html">http://developer.android.com/reference/android/widget/Button.html

下面是一个博客文章,他们认为的onClick作为容易和改进为岗位1.6应用;

<一个href="http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html">http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html

  

此新功能减少了的   Java的数量和XML,你必须   写,让你有更多的时间   专注于你的应用程序。

解决方案

我相信,列入机器人:的onClick 是一个非常糟糕的主意。

  1. 您正在连接presentation与逻辑
  2. 除非您使用支持它的插件,你必须要记住,如果你决定改变你的方法名重构XML文件
  3. 这只是不清楚你的XML一个按钮,并在您的活性的方法,即反应的单击事件,除非你明确地看到它在你的Java文件中定义的关系。随着安卓的onClick 接近你甚至可以忘记,你有一个按钮,你的布局或者是处理方法的的onClick 事件。

我会建议你坚持以编程方式定义你的 OnClickListener 和保持严格分离的关切,科里Sunwold指出,在他的评论。

Will the performance be any better using onClick? If I use onClick I do not have to set an android:id (also avoid's a new View.OnClickListener), does this improve performance at all? Or is the same effect of a findViewById occuring behind the scenes?

This page gives both methods as an option but little guidance on any benifit.

http://developer.android.com/reference/android/widget/Button.html

Here's a blog post where they deem onClick as "easier" and an "improvement" for post 1.6 applications;

http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html

This new feature reduces both the amount of Java and XML you have to write, leaving you more time to concentrate on your application.

解决方案

I believe that the inclusion of android:onClick has been a very bad idea.

  1. You are coupling presentation with logic
  2. Unless you are using a plugin that supports it, you will have to remember to refactor the xml file if you decide to change your method name
  3. It's just not clear the relationship between a button in your xml and a method in your activity that reacts to the click events unless you explicitly see it defined in your Java file. With the android:onClick approach you can even forget that you have a button in your layout or which is the method that is handling its onClick event.

I would suggest you stick to defining your OnClickListeners programatically and keep a strict separation of concerns, as Corey Sunwold pointed out in his comment.

这篇关于在Android中;岂不是更好施加OnClickListener或使用安卓的onClick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆