@UiHandler的文档

Roa*_*alt 23 java gwt uibinder

我开始研究将GWT与UiBuilder结合使用.我有点疑惑你如何使用该@UiHandler(..)指令来制作简单的事件处理代码,如GWT文档中所述:

@UiHandler("button")
void handleClick(ClickEvent e) {
  Window.alert("Hello, AJAX");
}
Run Code Online (Sandbox Code Playgroud)

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

但是,你可以使用什么ListBox来获得一个项目被选中的事件?我可以在文档中的哪个位置看到这个?

Igo*_*mer 33

传递给@UiHandler注释的参数是要分配的相应字段的名称*Handler.因此,在这种情况下,你都分配ClickHandler到一个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 @UiHandler annotated method that has a ClickEvent parameter -> that must mean it's a ClickHandler (notice that the method's name doesn't matter). 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

但是,你可以使用什么ListBox来获得一个项目被选中的事件?我可以在文档中的哪个位置看到这个?

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

  1. 你可以检查一下*Handler你要添加的内容ChangeHandler
  2. 它有void onChange(ChangeEvent event)- 所以,你的方法需要一个ChangeEvent参数,应该是这样的:

    @UiHandler("listBox")
    void whateverName(ChangeEvent event) {
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 哇,这几乎听起来像魔术.我自己也不会想出那种通配符的行为. (2认同)