簡體   English   中英

GWT TextArea-調整大小處理程序

[英]GWT TextArea - Resize Handler

一些瀏覽器為HTML Textareas顯示一個調整文本框大小的句柄。 有什么辦法可以對GWT中的調整大小事件做出反應?

我用以下代碼嘗試過,但未觸發事件:

TextArea textArea = new TextArea();

textArea.addHandler(new ResizeHandler() {

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

    }
}, ResizeEvent.getType());

“看起來您不能專門將事件附加到調整文本區域的大小當調整窗口大小時,會觸發resize事件。

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

這個問題已經有兩年歷史了,但是對於由Google提出這個問題的人,我有一個解決方案。

您可以使用鼠標向下,鼠標移動和鼠標向上事件來響應調整文本區域的大小。 這是基本代碼:

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;
              }
            }
          }

      });
    }
  }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM