GWT将ClickHandler添加到DOM元素 [英] GWT adding a ClickHandler to a DOM element

查看:104
本文介绍了GWT将ClickHandler添加到DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个自定义小部件,它有一个ClickHandler。下面是这个例子:

  public class TestWidget extends Composite {

private static TestWidgetUiBinder uiBinder = GWT
.create(TestWidgetUiBinder.class);

接口TestWidgetUiBinder扩展了UiBinder< Widget,TestWidget> {
}

@UiField
按钮按钮;

public TestWidget(String firstName){
initWidget(uiBinder.createAndBindUi(this));
button.setText(firstName);
}

@UiHandler(button)
void onClick(ClickEvent e){
Window.alert(Hello!);
}

}

当我尝试添加如下这个小部件:

  TestWidget testWidget = new TestWidget(myTestWidget); 
RootPanel.get()。add(testWidget);

一切都很好。如果我点击我的按钮,我会收到我期望的消息。
然而,如果我像这样添加它:

  TestWidget testWidget = new TestWidget(myTestWidget); 
RootPanel.getBodyElement()。appendChild(testWidget.getElement());

我的点击事件未被触发。我很努力地理解为什么。
如果有人能够向我解释这个或者将我链接到一个资源,我可以阅读这个资源,这将是很好的。最后,我想知道是否有可能添加clickhandler后,我附加了子事件,如果这种方式被推荐。感谢它提前寻求帮助。



kuku

解决方案

在要添加到面板的小部件上调用 add() Widget.onAttach()。 onAttach会做一些工作来注册widget以接收事件。 appendChild()只是简单地将一个DOM元素附加到另一个DOM元素上,而不执行其他操作。您应该可以通过这样做来获取第二个事件中的事件:

 元素元素= testWidget.getElement(); 
RootPanel.getBodyElement()。appendChild(element);
DOM.sinkEvents(element,
)Event.getTypeInt(ClickEvent.getType()。getName())
| DOM.getEventsSunk(element);

然而,我没有测试过这个,我不建议你在实际的应用程序中使用它。使用 add ()绝对是首选,这样使用 appendChild()没有任何优势,并可能导致意外的行为。


lets say i have a custom widget which has a ClickHandler. Here's the example:

public class TestWidget extends Composite {

private static TestWidgetUiBinder uiBinder = GWT
        .create(TestWidgetUiBinder.class);

interface TestWidgetUiBinder extends UiBinder<Widget, TestWidget> {
}

@UiField
Button button;

public TestWidget(String firstName) {
    initWidget(uiBinder.createAndBindUi(this));
    button.setText(firstName);
}

@UiHandler("button")
void onClick(ClickEvent e) {
    Window.alert("Hello!");
}

}

When i try to add this Widget like this:

    TestWidget testWidget = new TestWidget("myTestWidget");
    RootPanel.get().add(testWidget);

everything is fine. If i click on my button i get the message i expect. However if i add it like this:

TestWidget testWidget = new TestWidget("myTestWidget");
    RootPanel.getBodyElement().appendChild(testWidget.getElement());

my click event is not being fired. I'm struggeling to understand why. It would be nice if someone could explain this to me or link me to an resource where i can read this up. Finally i would like to know if it is possible to add the clickhandler afterwards i appended the child event and if that way is recommended. Thanks it advance for help.

kuku

解决方案

When you call add(), Widget.onAttach() is called on the widget that is being added to the panel. onAttach does some work to register the widget to receive events. appendChild() simply attaches one DOM element to another and does nothing else. You should be able to get events working in the second case by doing this:

Element element = testWidget.getElement();
RootPanel.getBodyElement().appendChild(element);
DOM.sinkEvents(element,
    Event.getTypeInt(ClickEvent.getType().getName())
    | DOM.getEventsSunk(element);

However, I haven't tested this and I wouldn't recommend that you use it in a real application. Using add() is definitely preferred, using appendChild() in this way has no advantages and may lead to unexpected behaviour.

这篇关于GWT将ClickHandler添加到DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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