将菜单相关事件称为“回调函数”有意义吗?
我正在浏览 android 记事本教程: http://developer.android .com/resources/tutorials/notepad/notepad-ex2.html
在 Notepadv2 的第 5 步中,它显示:
onListItemClick() 是我们将重写的回调方法。
我对回调的概念有点陌生,但通过研究,我的理解是回调是您在一个进程中提供的代码,当另一个进程达到某个结果时执行该代码。是什么使这个特定事件成为回调,而不是其他事件?
这是一个回调,因为它仅在用户执行某些操作时调用(在本例中,单击列表项)?但如果是这样的话,任何事件都不能被视为回调,因为它仅在自身之外的事情发生时才会被调用吗?或者我错过了什么?
I'm going through the android notepad tutorials: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
In Notepadv2, Step 5, it says:
onListItemClick() is a callback method that we'll override.
I'm a bit new to the concept of callbacks, but from researching, my understanding is that a callback is code you provide in one process that's executed when another process reaches a certain result. What makes this particular event a callback, and not other events?
Is this a callback because it's only called when the user does something (in this case, click on a list item)? But if that's so, can't any event be considered a callback in that it's only called when something outside of itself happens? Or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不完全正确。
Wikipedia 将回调描述为“对可执行代码的引用,或一段可执行文件代码,作为参数传递给其他代码”。这可能是这个词的最佳用法。在这种情况下,由于 Java 操作以方法的形式出现,因此“回调方法”是将作为回调结果而被调用的方法。
不完全是。
它是一个回调方法,因为 Android 框架的其他部分正在检测事件(在本例中,用户点击列表中的某个项目)并调用
onListItemClick()
回调方法来为您提供控制。通常,“回调”是为检测事件的模块与实现回调以处理事件的模块之间存在明确定义的不透明边界的情况而保留的。在本例中,您没有编写 Android 操作系统,因此
onListItemClick()
是对您自己的代码的回调,操作系统将调用该回调来让您处理列表项点击。Not exactly.
Wikipedia describes a callback as "a reference to executable code, or a piece of executable code, that is passed as an argument to other code". That's probably the best use of the term. In this case, since Java operations come in the form of methods, a "callback method" is a method that will be invoked as a result of a callback.
Not exactly.
It is a callback method because other pieces of the Android framework are detecting an event (in this case, the user tapping on an item in a list) and are calling the
onListItemClick()
callback method to give you control.Usually, "callback" is reserved for cases where there's a clearly defined opaque boundary between the module detecting the event and the module implementing the callback to process the event. In this case, you did not write the Android operating system, and so
onListItemClick()
is a callback into your own code that the OS will call to let you process list item clicks.CommonsWare 的答案要完整得多,但我想我可以稍微简化一下解释。
回调是被回调的东西。从概念上讲,您声明您对特定事件感兴趣(例如,单击按钮、某个任务完成时、经过一段时间等)并指示您的代码给您回电当它发生时。
这就像拿起电话并告诉某人“准备好后给我回电话”。回调不会采用电话调用的形式,而是函数调用的形式。
您调用一个函数并给它一个回调,该函数在适当的时候回调您的回调。
CommonsWare answer is far more complete, but I think I can simplify the explanation a little.
A callback is something that gets called back. Conceptually, you declare that you're interested in a particular event (e.g. a button click, when a certain task finishes, an amount of time went by, etc.) and instruct your code to call you back when it occurs.
It's just like picking up the phone and telling someone "call me back when you're ready". The callback won't take the form of a phone call, but of a function call.
You call a function and hand it a callback, and that function calls back your callback when appropiate.