在哪里设置所有侦听器? [英] Where to set all Listeners?

查看:146
本文介绍了在哪里设置所有侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里为用户界面设置所有Listeners?
onCreate中设置它们是一种好习惯吗?这看起来是如此结构混乱和奇怪.
有更好的设置它们的地方吗?

Where to set all Listeners for the user interfaces?
Is it good practice to set them in onCreate? This looks so unstructured and strange.
Is there a better place to set them?

推荐答案

从此处: http://developer.android.com/reference/android/app/Activity.html

onCreate(Bundle)是您初始化活动的地方.最多 重要的是,在这里您通常会使用 布局资源,用于定义您的UI,并使用findViewById(int)来 在您需要与之交互的UI中检索小部件 以编程方式.

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

初始化视图时,即可随时收听它们. onCreate是设置侦听器的良好回调.您可以通过其他方式在onStartonResume中进行设置,但您应该了解这是一种不好的做法,因为onStartonResume每次在用户看到您的活动时都会调用. onCreate仅在初始化Activity时调用.这就是为什么要使用onCreate的原因.实际上,良好实践会实现类似initListeners()的方法,您可以在其中放置所有侦听器逻辑.

When you initialize your views, they are ready to be listened. onCreate is good callback to set listeners. In other way you can set it in onStart or onResume, but you should understand, that its bad practice, because onStart and onResume calls every time, when user see your activity. onCreate calls only when Activity is initialized. It is reason, why you should use onCreate. Actually, good practice implement method like initListeners() where you can put all you listeners logic.

祝你好运!

这篇关于在哪里设置所有侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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