Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

performance - In Android; is it better applying an OnClickListener or use android:onClick?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...