java @UiHandler 的文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3440763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:52:43  来源:igfitidea点击:

Documentation for @UiHandler

javagwtuibinder

提问by Roalt

I started to look into using GWT in combination with UiBuilder. I'm a bit puzzled about how you can use the @UiHandler(..)directive to make simple event handle code as written down in the GWT documentation:

我开始研究将 GWT 与 UiBuilder 结合使用。我对如何使用该@UiHandler(..)指令来制作GWT 文档中所写的简单事件句柄代码感到有些困惑:

@UiHandler("button")
void handleClick(ClickEvent e) {
  Window.alert("Hello, AJAX");
}

In this case the method handleClickis used. How do you know for each GWT widget what methods can be created with @UiHandler? For some you can also create a doClose()method.

在这种情况下,handleClick使用该方法。您如何知道每个 GWT 小部件可以使用哪些方法创建@UiHandler?对于某些人,您还可以创建一个doClose()方法。

But what can you use with, for instance, a ListBoxto get an event an item is selected? Where in the documentation can I see this?

但是,例如,您可以使用 aListBox来获取选择项目的事件吗?我可以在文档中的哪个位置看到这个?

回答by Igor Klimer

The parameter you pass to the @UiHandlerannotation is the name of the appropriate field you want to assign that *Handler. So, in this case you are assigning a ClickHandlerto a Button button(actually, we just know the field's name).

您传递给@UiHandler注释的参数是您要为其分配的适当字段的名称*Handler。因此,在这种情况下,您将 a 分配ClickHandler给 a Button button(实际上,我们只知道字段的名称)。

As for how this exactly works - it's part of GWT magic :) My guess is that, just like any other UiBinder related code (I think there was a presentation on Google IO, that showed the code that UiBinder generates), at compilation time the compiler figures out what goes where. In this example: we have a Button button, and we have a @UiHandlerannotated method that has a ClickEventparameter -> that mustmean it's a ClickHandler(notice that the method's name doesn'tmatter). So let's add some code at compile time (in the constructor, probably) that adds that handler to the button. If you are interested in a more comprehensive answer - check out the source :D

至于这究竟是如何工作的——它是 GWT 魔法的一部分 :) 我的猜测是,就像任何其他 UiBinder 相关代码一样(我认为 Google IO 上有一个演示文稿,展示了 UiBinder 生成的代码),在编译时编译器计算出什么去哪里。在这个例子中:我们有一个Button button,我们有一个@UiHandlerClickEvent参数的带注释的方法->必须表示它是一个ClickHandler(注意方法的名称并不重要)。因此,让我们在编译时(可能在构造函数中)添加一些代码,将处理程序添加到按钮。如果您对更全面的答案感兴趣 - 查看来源:D

But what can you use with, for instance, a ListBoxto get an event an item is selected? Where in the documentation can I see this?

但是,例如,您可以使用 aListBox来获取选择项目的事件吗?我可以在文档中的哪个位置看到这个?

In the GWT API reference. In this case, you are probably looking for ListBox.addChangeHandler. But you usually won't find @UiHandlerrelated code there - that's because it would be redundant - you always construct the @UiHandlermethods the same way:

GWT API 参考中。在这种情况下,您可能正在寻找ListBox.addChangeHandler。但是你通常不会在@UiHandler那里找到相关的代码——那是因为它是多余的——你总是@UiHandler以相同的方式构造方法:

  1. You check the *Handlerthat you want to add, say ChangeHandler
  2. It has a void onChange(ChangeEvent event)- so, your method needs a ChangeEventparameter and should look like this:

    @UiHandler("listBox")
    void whateverName(ChangeEvent event) {
        // ...
    }
    
  1. 你检查*Handler你想要添加的,说ChangeHandler
  2. 它有一个void onChange(ChangeEvent event)- 所以,你的方法需要一个ChangeEvent参数,应该是这样的:

    @UiHandler("listBox")
    void whateverName(ChangeEvent event) {
        // ...
    }
    

回答by Fabricio

Probably your problem is in your onModuleLoad method:

您的问题可能出在 onModuleLoad 方法中:

public void onModuleLoad() 
{       
    HelloWorld helloWorld = new HelloWorld("BOTAO"); 

    // Using this way @UiHandler will not work
    //Document.get().getBody().appendChild(helloWorld.getElement()); 

    // correct way
    RootPanel.get().add(helloWorld);  
}