本文介绍了处理GWT中的DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在buttonelement中添加处理程序,我已经实现了如下。请帮我解决这段代码中的错误。我不想直接在按钮小部件上添加处理程序。
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);
推荐答案
你的代码是正确的,你可以在下沉后添加小部件事件。你必须在sink事件之前添加小部件。例如:
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元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!