如何定义回调中的Andr​​oid?回调、定义、oid、Andr

由网友(养条狗取名前男友)分享简介:在最近的谷歌IO出现了有关实现RESTful客户端应用程序presentation。不幸的是这只是没有源$ C ​​$实施℃的高层次讨论。有一个棘手的问题对我来说,我似乎无法找到有关的任何信息,这是没有必要看到了presentation要能够回答这个问题。在这个图( http://i.imgur.com/GlYQF.g...

在最近的谷歌IO出现了有关实现RESTful客户端应用程序presentation。不幸的是这只是没有源$ C ​​$实施℃的高层次讨论。有一个棘手的问题对我来说,我似乎无法找到有关的任何信息,这是没有必要看到了presentation要能够回答这个问题。在这个图( https://p.xsw88.cn/allimgs/daicuo/20230911/3939.png.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 ( https://p.xsw88.cn/allimgs/daicuo/20230911/3939.png.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.

阅读全文

相关推荐

最新文章