[英]GWT event handler not firing
Do you have any clues why the event handler does not fire ? 您有什么线索为什么事件处理程序不触发?
Maybe I'm getting something wrong with event bubbling and gwt ? 也许事件冒泡和gwt出了点问题?
public class MyNavBarUI extends Composite
{
@UiTemplate("MyNavBarUI.ui.xml")
interface MyUiBinder extends UiBinder<Widget, MyNavBarUI>
{}
@UiField
HTML html;
@UiField
HTMLPanel htmlp;
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
public MyNavBarUI()
{
// sets listBox
initWidget(uiBinder.createAndBindUi(this));
html.setHTML(
"<div id = \"ul_ud1\"> Home </div> "
+ "<div id = \"ul_ud2\"> News </div>"
+ "<div id = \"ul_ud3\"> Contact </div>"
+ "<div id = \"ul_ud4\"> About </div>"
);
}
@Override
public void onAttach()
{
super.onAttach();
Event.setEventListener(htmlp.getElementById("ul_ud1"), new EventListener() {
@Override
public void onBrowserEvent(Event event) {
Window.alert(""+event.toString());
}
});
}
}
You just need to sink events: 您只需要接收事件:
Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK);
You can sink multiple events, for example 您可以接收多个事件,例如
Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK | Event.ONMOUSEOVER);
Then, in the EventListener you can check the type of event: 然后,在EventListener中,您可以检查事件的类型:
public void onBrowserEvent(Event event) {
switch(event.getTypeInt()) {
case Event.ONCLICK:
Window.alert("Click");
break;
case Event.ONMOUSEOVER:
Window.alert("Mouse over");
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.