GWT 中 DOM 元素的处理程序 [英] Handler on DOM elements in GWT
本文介绍了GWT 中 DOM 元素的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按钮元素上添加处理程序,我已按如下方式实现它.请帮助我解决此代码中的错误.我不想直接在按钮小部件上添加处理程序.
I want to add the handler on the buttonelement and i have implemented it as follow. Please help me in resolving the error in this code. I do not want to add handler directly on the button widget.
Button button = new Button("Click");
Element buttonElement = button.getElement();
Event.setEventListener(buttonElement, new EventListener() {
@Override
public void onBrowserEvent(Event event) {
String string = event.getType();
if(string.equalsIgnoreCase("click")) {
System.out.println("CLICK");
}
}
});
Event.sinkEvents(buttonElement, Event.ONCLICK);
推荐答案
您的代码是正确的,您可能在接收器事件后添加了小部件.您必须在接收器事件之前添加小部件.举个例子:
Your code is correct, you might added widget after sink event. you have to add widget before sink event. just example:
Button button=new Button("Click");
Element buttonElement = button.getElement();
RootPanel.get().add(button);
Event.sinkEvents(buttonElement, Event.ONCLICK);
Event.setEventListener(buttonElement, new EventListener() {
@Override
public void onBrowserEvent(Event event) {
System.out.println("ok");
if(Event.ONCLICK == event.getTypeInt()) {
Window.alert("ok");
System.out.println("CLICK");
}
}
});
这篇关于GWT 中 DOM 元素的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文