如何定义回调中的Andr​​oid? [英] How to Define Callbacks in Android?

查看:152
本文介绍了如何定义回调中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的谷歌IO出现了有关实现RESTful客户端应用程序presentation。不幸的是这只是没有源$ C ​​$实施℃的高层次讨论。有一个棘手的问题对我来说,我似乎无法找到有关的任何信息,这是没有必要看到了presentation要能够回答这个问题。在这个图( http://i.imgur.com/GlYQF.gif 的返回路径上有有各种不同的回调的其他方法。我不明白的是我如何声明一下这些方法。换句话说,我明白一个回调(一张code被调用某个事件之后发生)的想法,但我不知道如何实现它,我一直没能找到一个合适的说明Android的在线呢。我实现了回调,至今已覆盖各种方法的唯一途径(onActivityResult为例)。

During the most recent google io there was a presentation about implementing restful client applications. Unfortunately it was only a high level discussion with no source code of the implementation. There is one sticking point for me that I can't seem to find any information about and it's not necessary to have seen the presentation to be able to answer this question. In this diagram ( http://i.imgur.com/GlYQF.gif ) on the return path there are various different callbacks to other methods. What I don't understand is how I declare what these methods are. In other words I understand the idea of a callback (a piece of code that gets called after a certain event has happened), but I don't know how to implement it and I haven't been able to find a suitable explanation for android online yet. The only way I've implemented callbacks so far have been overriding various methods (onActivityResult for example).

我觉得我有设计模式的一个基本的了解,但我一直在得到绊倒如何处理返回路径。感谢您的任何帮助。

I feel like I have a basic understanding of the design pattern, but I keep on getting tripped up on how to handle the return path. Thank you for any help.

推荐答案

在很多情况下,你有一个接口,以及一个对象来实现它传递。对话框,例如有OnClickListener。

In many cases, you have an interface and pass along an object that implements it. Dialogs for example have the OnClickListener.

正如随便举个例子:

// The callback interface
interface MyCallback {
    void callbackCall();
}

// The class that takes the callback
class Worker {
   MyCallback callback;

   void onEvent() {
      callback.callbackCall();
   }
}

// Option 1:

class Callback implements MyCallback {
   void callbackCall() {
      // callback code goes here
   }
}

worker.callback = new Callback();

// Option 2:

worker.callback = new MyCallback() {

   void callbackCall() {
      // callback code goes here
   }
};

我可能搞砸的选项2.语法还早。

I probably messed up the syntax in option 2. It's early.

这篇关于如何定义回调中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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