我需要向文档注册一个跨平台和版本无关的单击事件。
这意味着我有两个文本框和提交按钮,但是当我在两个文本框之外单击并提交按钮时,
将显示警报。我如何通过gwt实现此功能
document.get()。addMouseClick ???
最佳答案
我想到的最简单的方法是将所有内容包装在 FocusPanel
中:
ClickHandler clickHandler = new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("TextBox/Button clickHandler.");
event.stopPropagation(); // The important line - We stop the event
// propagation here so that the FocusPanel
// doesn't get the event
}
};
TextBox textBox = new TextBox();
textBox.addClickHandler(clickHandler);
Button button = new Button("Test");
button.addClickHandler(clickHandler);
// Since FocusPanel is a SimplePanel, it can only have one child, so we are
// wrapping everything additionally in a HorizontalPanel
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.add(textBox);
hPanel.add(button);
FocusPanel focusPanel = new FocusPanel(hPanel);
focusPanel.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Outside."); // Clicked outside of the TextBox/Button
}
});
RootPanel.get().add(focusPanel);
缺点是,您需要为不需要警报的每个元素分配
ClickHandler
(可以使用相同的ClickHandler
来节省内存,就像我上面所做的那样)。除此之外, FocusPanel
实现应确保onclick行为保持跨浏览器。
关于gwt - gwt点击事件文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2662877/