【问题标题】:GWT TextArea - Resize HandlerGWT TextArea - 调整大小处理程序
【发布时间】:2013-08-13 21:06:34
【问题描述】:

某些浏览器会为 HTML 文本区域显示调整文本框大小的句柄。有什么方法可以对 GWT 中的这种调整大小事件做出反应?

我用这段代码试过了,但没有触发事件:

TextArea textArea = new TextArea();

textArea.addHandler(new ResizeHandler() {

    @Override
    public void onResize(ResizeEvent event) {
        System.out.println("resize");

    }
}, ResizeEvent.getType());

【问题讨论】:

标签: java javascript html gwt


【解决方案1】:

“看起来您不能专门将事件附加到调整文本区域的大小。调整窗口大小时会触发 resize 事件。

https://stackoverflow.com/a/2096352/1467482

【讨论】:

  • 如果你发现了相同的帖子,由于你没有足够的声望,你应该把它标记为重复,或者用那个问题发表评论。
  • 在我看来,这不是真正的复制品。我认为这个问题对于没有 Javascript 知识的 GWT 设计者很有用。
【解决方案2】:

这个问题已经有两年了,但对于被谷歌带到这个问题的人,我有一个解决方案。

您可以使用mouse-down、mouse-move 和mouse-up 事件对文本区域的大小调整做出反应。这是一个骨架代码:

TextArea textArea = new TextArea();
...
textArea.addMouseDownHandler(new MouseDownHandler() {
  private HandlerRegistration mouseMoveUpRegistration;
  private int lastWidth;
  private int lastHeight;

  @Override
  public void onMouseDown(MouseDownEvent event) {
    lastWidth = getOffsetWidth();
    lastHeight = getOffsetHeight();

    if (mouseMoveUpRegistration == null) {
      mouseMoveUpRegistration = Event.addNativePreviewHandler(new NativePreviewHandler() {
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
          if (event.getTypeInt() == Event.ONMOUSEMOVE || event.getTypeInt() == Event.ONMOUSEUP) {
            int width = getOffsetWidth();
            int height = getOffsetHeight();
            if (width != lastWidth || height != lastHeight) {
              // do whatever you want to to while still resizing
              lastWidth = width;
              lastHeight = height;
            }

            if (event.getTypeInt() == Event.ONMOUSEUP) {
              // do whatever you want to do when resizing finished
              // perhaps check actual and initial size to see if the size really changed
              /* mouse up => additionally remove the handler */
              if (mouseMoveUpRegistration != null) {
                mouseMoveUpRegistration.removeHandler();
                mouseMoveUpRegistration = null;
              }
            }
          }

      });
    }
  }

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多