gwt点击事件文件

时间:2010-04-18 16:03:07

标签: gwt events click document

我需要在文档中注册一个跨平台和版本无关的点击事件。 这意味着我有一个两个文本框和提交按钮,但当我点击两个文本框外面然后提交按钮 将显示警报。如何通过gwt

实现这一目标

document.get()。addMouseClick ???

1 个答案:

答案 0 :(得分:1)

想到的最简单的方法是将所有内容包装在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行为保持跨浏览器。