什么是实现一个OnClickListener的正确方法 [英] What is the correct way to implement an OnClickListener

查看:223
本文介绍了什么是实现一个OnClickListener的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过实施中的Andr​​oid的OnClickListener的许多方面,我想知道是否有一个最佳实践或做它比其他的更推荐的方法吗?(即:我记得读某些方面需要比别人更多的内存)

With so many ways of implementing an OnClickListener within Android, I'm wondering whether there's a best practice or a more recommended way of doing it over the others (ie: I remember reading certain ways require more memory than others)?

目前我知道的四种方式来实现OnClickListener的时刻,它们是:

At the moment I know of four ways to implement the OnClickListener, these are:


  1. 请您的活动实现一个OnClickListener接口。

  2. 内部类OnClickListener。

  3. 内嵌类OnClickListener。

  4. 使用Android的:在一个按钮的XML定义onclick属性

在对四个选项我倾向于XML实现,因为它似乎更清洁,可别人给他们的意见?

Out of the four options I'm leaning towards the XML implementation as it seems cleaner, can anyone else give their opinion?

推荐答案

我不知道对于内存的效率,但这里是我的方法。

I don't know regarding memory efficiency, but Here's my approach.


  1. 我不喜欢它,它需要多个的if-else(或交换机)内的的onClick 如果您有多个按钮

  2. 我用这个,如果第三个选项会导致我的方法,例如的onCreate()来过大和杂乱

  3. 我最喜欢的。它可以让你找出每一个按钮很容易做,但我用它通常如果的onClick 不是太长,要保持code可读

  4. 我几乎不使用它,它保持了code清洁,但我不习惯这个,因为我没有在Java的SWING使用它。

  1. I don't like it, It requires multiple if-else (or switch) inside your onClick if you have multiple buttons
  2. I use this if the 3rd option causes my method, for example onCreate() to be too big and messy
  3. My favorite. it allows you to find out what each button does very easily, but I use it usually if its onClick isn't too long, to keep the code readable
  4. I hardly use it, it keeps the code cleaner, but I'm not used to this one, since I don't use it in Java's SWING.

但在底线,就像@Lazy_Ninja说,这一切都归结到味道。他们的所有作品4

But in the bottom line, like @Lazy_Ninja said, it all comes down to taste. All 4 of them works.

我觉得重要的,选择时,是保持code干净和可读性。

I think what matters, when choosing, is keeping the code clean and readable.

这篇关于什么是实现一个OnClickListener的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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