“XYZ”回调事件如何工作?

时间:2022-03-16 21:00:59

I'm looking for concept explanations or example code, if possible

如果可能的话,我正在寻找概念解释或示例代码

I'm new to (android) development and from what i've seen, there are a ton of "onEvent()" methods. for example, View.onClickListener(), onPageDownload, onProgressUpdate... i've noticed that everything starts with "on", and i'm assuming that it implies the method is a callback event? is that the same thing as a handler?

我是(android)开发的新手,从我所见,有大量的“onEvent()”方法。例如,View.onClickListener(),onPageDownload,onProgressUpdate ...我注意到一切都以“on”开头,我假设它暗示该方法是一个回调事件?和处理程序一样吗?

i have a hard time understanding how events work, and what's the best way to make my own event handler.

我很难理解事件是如何工作的,以及制作自己的事件处理程序的最佳方法是什么。

say i want to make a handler onImageDownloaded() that gets called when a certain event happens. like perhaps i'm downloading an image, and i want to trigger an event when the image downloads (and yes, i get that there is something called AsyncTask and also posting a runnable message from background thread, but for pedagogical reasons let's pretend the process could have been on the UI thread also). How does Android know to call the method onImageDownloaded() ? or rather, how do i specify that when an image gets downloaded, i want it to call my method onImageDownloaded() ?

说我想在一个事件发生时调用onImageDownloaded()来处理它。也许我正在下载一个图像,我想在图像下载时触发​​一个事件(是的,我得到的东西叫做AsyncTask并且还从后台线程发布了一个可运行的消息,但出于教学原因让我们假装这个过程也可以在UI线程上)。 Android如何知道调用onImageDownloaded()方法?或者更确切地说,我如何指定在下载图像时,我希望它在onImageDownloaded()上调用我的方法?

i've read about broadcast intents, but that seems more of a system level and kind of over-doing it.

我已经阅读过关于广播意图的内容,但这似乎更像是一个系统级别,并且有点过分了。

I know that when you make a button in the view, you can do

我知道当你在视图中创建一个按钮时,你可以做到

final Button b = (Button) findViewById(R.id.mybutton);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("MyActivity", "button is clicked");
    }
});

and it's almost like setting a "hook" to the button that calls the particular "onClick" when the click event happens.

这就像在按钮事件发生时调用特定“onClick”的按钮设置一个“钩子”。

in my particular case, how would i create a custom callback method upon actions "XYZ" ??

在我的特定情况下,如何在动作“XYZ”上创建自定义回调方法?

sorry for the long question, thanks for the patience to read and to try and understand it

抱歉这个长期的问题,感谢您耐心阅读并尝试理解它

3 个解决方案

#1


3  

We need to first learn what is callback:

我们需要先了解什么是回调:

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

在计算机编程中,回调是对可执行代码或一段可执行代码的引用,它作为参数传递给其他代码。这允许较低级别的软件层调用在较高级别层中定义的子例程(或函数)。

Wickipedia: Callback

Wickipedia:回调

To make our code more re-useable and optimize, we need call back.

为了使我们的代码更易于重用和优化,我们需要回电。

Let's say: you have a class which downloads data from the internet. If you want to make the class more re-useable, you will define a protocol (any class construct) to whom it will inform. Later the classes which conforms to that protocol (or implements that protocol)can use the downloaded data accordingly.

让我们说:你有一个从互联网上下载数据的课程。如果您希望使类更具可重用性,您将定义一个协议(任何类构造),它将通知它。稍后,符合该协议(或实现该协议)的类可以相应地使用下载的数据。

Callbacks are made typically using interface keyword, but abstract class or a simple class can be used also.

回调通常使用interface关键字进行,但也可以使用抽象类或简单类。

Here is simple illustration how to make and use our own callbacks.

以下是如何制作和使用我们自己的回调的简单说明。

#2


2  

A simple example of what is going on:

发生了什么的一个简单例子:

XYZer xyzer=new XYZer();
xyzer.setXYZListener(new XYZListener() {

    public void onXYZ(View v) {
        Log.d("MyActivity", "XYZ happened");
    }
});

class XYZer
{
    XYZListener listener;
    void setXYZListener(XYZListener listener){this.listener=listener;}

    void test(View v)
    {
        if(listener!=null)listener.onXYZ(v);
    }
}

HTH. Sorry I haven't got time for a more long winded explanation, hopefully someone else will provide that.

HTH。对不起,我没有时间进行更长时间的解释,希望其他人能提供。

#3


1  

One way of achieving the callback mechanism is by using an Interface.

实现回调机制的一种方法是使用接口。

The following tutorials contains a nice example of a callback mechnanism.

以下教程包含一个回调mechnanism的好例子。

http://about-android.blogspot.com/2010/02/create-custom-dialog.html .

http://about-android.blogspot.com/2010/02/create-custom-dialog.html。

have a look at the interface ReadyListener which has a single method

看一下具有单一方法的ReadyListener接口

public void ready(String name);

#1


3  

We need to first learn what is callback:

我们需要先了解什么是回调:

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

在计算机编程中,回调是对可执行代码或一段可执行代码的引用,它作为参数传递给其他代码。这允许较低级别的软件层调用在较高级别层中定义的子例程(或函数)。

Wickipedia: Callback

Wickipedia:回调

To make our code more re-useable and optimize, we need call back.

为了使我们的代码更易于重用和优化,我们需要回电。

Let's say: you have a class which downloads data from the internet. If you want to make the class more re-useable, you will define a protocol (any class construct) to whom it will inform. Later the classes which conforms to that protocol (or implements that protocol)can use the downloaded data accordingly.

让我们说:你有一个从互联网上下载数据的课程。如果您希望使类更具可重用性,您将定义一个协议(任何类构造),它将通知它。稍后,符合该协议(或实现该协议)的类可以相应地使用下载的数据。

Callbacks are made typically using interface keyword, but abstract class or a simple class can be used also.

回调通常使用interface关键字进行,但也可以使用抽象类或简单类。

Here is simple illustration how to make and use our own callbacks.

以下是如何制作和使用我们自己的回调的简单说明。

#2


2  

A simple example of what is going on:

发生了什么的一个简单例子:

XYZer xyzer=new XYZer();
xyzer.setXYZListener(new XYZListener() {

    public void onXYZ(View v) {
        Log.d("MyActivity", "XYZ happened");
    }
});

class XYZer
{
    XYZListener listener;
    void setXYZListener(XYZListener listener){this.listener=listener;}

    void test(View v)
    {
        if(listener!=null)listener.onXYZ(v);
    }
}

HTH. Sorry I haven't got time for a more long winded explanation, hopefully someone else will provide that.

HTH。对不起,我没有时间进行更长时间的解释,希望其他人能提供。

#3


1  

One way of achieving the callback mechanism is by using an Interface.

实现回调机制的一种方法是使用接口。

The following tutorials contains a nice example of a callback mechnanism.

以下教程包含一个回调mechnanism的好例子。

http://about-android.blogspot.com/2010/02/create-custom-dialog.html .

http://about-android.blogspot.com/2010/02/create-custom-dialog.html。

have a look at the interface ReadyListener which has a single method

看一下具有单一方法的ReadyListener接口

public void ready(String name);