您是否知道事件处理程序不启动的原因
也许我在事件冒泡和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());
}
});
}
}
您只需要接收事件:
Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK);
您可以接收多个事件,例如
Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK | Event.ONMOUSEOVER);
然后,在EventListener中,您可以检查事件的类型:
public void onBrowserEvent(Event event) {
switch(event.getTypeInt()) {
case Event.ONCLICK:
Window.alert("Click");
break;
case Event.ONMOUSEOVER:
Window.alert("Mouse over");
break;
}
}