1

我有一个表单,我想在用户按下后退按钮时显示一个确认对话框。假设我有一个听 ChangeValueHandler 的 Texbonx

addValueChangeHandler(new ValueChangeHandler<String>() {    
    @Override
    public void onValueChange(ValueChangeEvent<String> event) {
        setChanged(true);
    }
}); 

这是简短的场景

1)我在文本框中输入文本

2)我点击后退按钮

3)ValueChangeEvent未被调用

BlurEvent我在 TextBox 上以编程方式尝试着火,textBox.fireEvent(new BlurEvent() { });但仍然没有结果。

有什么建议么 ?

4

2 回答 2

1

When the page is closed (by pressing the back button, or closing the page) no events will be fired by the controls. What will fire first is window.onbeforeunload to give you a chance to warn the user about data loss and offer to stay on the page. If the user chooses to stay on the page, then all the events that were supposed to be fired, will fire (so your change event will fire).

You can attach a handler to the native onbeforeunload event by using Window.addClosingHandler.

Window.addWindowClosingHandler(new ClosingHandler() {

        @Override
        public void onWindowClosing( ClosingEvent event )
        {
            event.setMessage("If you leave the page now all data will be lost.");
        }

});

It's worth noting that the ClosingEvent and it's underlying onbeforeunload event, cannot, under any circumstances, be cancelled programmatically. The only way to prevent the user from leaving the page is if the user itself chooses "Stay On This Page" in the popup that results from the code above.

于 2012-07-24T00:13:14.220 回答
0

我所做的是将 TextBox 的焦点设置为 False,然后检查它是否已更改,这会在单击后退按钮时强制 TextBox 不聚焦。

这是检查表单是否更改的代码

public boolean isChanged(){
    if(formPanel == null) return false;     
    for (int i = 0; i < formPanel.getWidgetCount(); i++) {
        if(formPanel.getWidget(i) instanceof BaseWidget){
            BaseWidget w= (BaseWidget) formPanel.getWidget(i);
            w.setFocus(false);
            if(w.isChanged()){
                return true;
            }
        }
    }
    return false;
}
于 2012-07-29T20:59:46.903 回答