繁体   English   中英

Clickhandler不会触发

[英]Clickhandler doesn't fire

我在使用GWT的项目中ClickHandler出现问题。

我想在对话框的标题中插入一个新按钮。

  1. 我创建了一个新的插入方法: addToTitle(...)
  2. 我在按钮上添加了ClickHandler

问题:按按钮单击事件不会触发。 为什么?

这是我的代码:

DialogBox dialog = new DialogBox();

Button button = new Button("A new Button");

       button.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                Window.alert("yuhuhuhu");

            }
        });

dialog.addToTitle(button);

代码(从注释部分中提取):

public class PlentyDialogWindow extends DialogBox { 
    private FlowPanel captionPanel = new FlowPanel(); 
    public Widget closeWidget = null; 
    private boolean closeOnEscKey = false; 
    private FlowPanel titleContentWrapper = new FlowPanel(); 
    public PlentyDialogWindow(boolean isModal) { 
        super( false, isModal); 
        this.addStyleName("DialogBox"); 
        this.getElement().setId("DialogBoxId"); 
        this.setAnimationEnabled(true); 
        this.closeWidget = generateCloseButton(); 
    }

    public void setCaption( String txt,Widget w) { 
        captionPanel.setWidth("100%"); 
        this.addCaption(txt); 
        this.titleContentWrapper.getElement().getStyle().setDisplay(Display.INLINE_BLOCK); 
        captionPanel.add(this.titleContentWrapper); 
        FlowPanel widgetWrapper = new FlowPanel(); 
        widgetWrapper.add(w); 
        widgetWrapper.addStyleName("PlentyPopupCloseIconWrapper"); 
        captionPanel.add(widgetWrapper); 
        captionPanel.addStyleName("Caption"); 
        Element td = getCellElement(0,1); 
        td.setInnerHTML(""); 
        td.appendChild(captionPanel.getElement()); 
    }

    /** * * @param w */ public void addToTitle(Widget w) { 
        this.titleContentWrapper.add(w); 
    } 
}

解决方案有点棘手。

public class PlentyDialogWindow extends DialogBox {

    /* 
     * Create custom inner class extending `FlowPanel`. You need it only
     * to make `onAttach` and `onDetach` methods be visible to wrapping 
     * class (e.g. your `PlentyDialogWindow` class).
     */
    static class MyCaptionPanel extends FlowPanel {

        @Override
        protected void onAttach() {
            super.onAttach();
        }

        @Override
        protected void onDetach() {
            super.onDetach();
        }
    }

    /* 
     * `PlentyDialogWindow`'s field `captionPanel` will be an instance of 
     * this class.
     */
    private MyCaptionPanel captionPanel = new MyCaptionPanel();


    /*
     * ... leave the rest of your class untouched ...
     */


    /*
     * Finally, overwrite `PlentyDialogWindow`'s `onAttach` and `onDetach` 
     * methods to invoke `captionPanel`'s corresponding methods:
     */
    @Override
    protected void onAttach() {
        super.onAttach();
        captionPanel.onAttach();
    }

    @Override
    protected void onDetach() {
        super.onDetach();
        captionPanel.onDetach();
    }
}

就这样。

如果您唯一的问题是没有调用ClickHandler,请尝试使用addDOMHandler而不是addClickHandler

yourWidget.addDomHandler(new ClickHandler() {

  @Override
  public void onClick(ClickEvent event) {


  }
},ClickEvent.getType());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM