Exemplo n.º 1
0
    /** 覆寫觸控事件,分辨出是否為連結的點擊。 */
    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
      int action = event.getAction(); // 取得事件類型

      if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length != 0) { // 是連結點擊
          if (widget instanceof LinkTextView) { // 如果實體是LinkTextView
            ((LinkTextView) widget).linkHit = true;
          }
          if (action == MotionEvent.ACTION_UP) { // 放開時
            //                        link[0].onClick(widget); // 開啟連結
            String url = ((URLSpan) link[0]).getURL();
            Utils.clickUrl(widget, url);
          } else if (action == MotionEvent.ACTION_DOWN) { // 按下時
            Selection.setSelection(
                buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); // 選擇連結
          }
          return true;
        } else { // 不是連結點擊
          if (widget instanceof LinkTextView) { // 如果實體是LinkTextView
            ((LinkTextView) widget).linkHit = false;
          }
          Selection.removeSelection(buffer);
          Touch.onTouchEvent(widget, buffer, event);
          return false;
        }
      }
      return Touch.onTouchEvent(widget, buffer, event);
    }
  @Override
  public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (mGray == null) {
      mGray =
          new BackgroundColorSpan(
              widget.getContext().getResources().getColor(R.color.selector_gray));
    }

    mIsLinkHit = false;

    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
      int x = (int) event.getX();
      int y = (int) event.getY();

      /*if (DEBUG) {
      	Log.d(TAG, "x = " + x + " y = " + y);
      }*/

      x -= widget.getTotalPaddingLeft();
      y -= widget.getTotalPaddingTop();

      /*if (DEBUG) {
      	Log.d(TAG, "x = " + x + " y = " + y);
      }*/

      x += widget.getScrollX();
      y += widget.getScrollY();

      int line = widget.getLayout().getLineForVertical(y);
      int offset = widget.getLayout().getOffsetForHorizontal(line, x);

      ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);

      /*if (DEBUG) {
      	Log.d(TAG, "x = " + x + " y = " + y);
      	Log.d(TAG, "line = " + line + " offset = " + offset);
      	Log.d(TAG, "spans.lenth = " + spans.length);
      }*/

      if (spans.length != 0) {
        int start = buffer.getSpanStart(spans[0]);
        int end = buffer.getSpanEnd(spans[0]);

        mIsLinkHit = true;

        if (action == MotionEvent.ACTION_DOWN) {
          /*if (DEBUG) {
          	Log.d(TAG, "Down event detected");
          }*/

          buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (action == MotionEvent.ACTION_UP) {
          /*if (DEBUG) {
          	Log.d(TAG, "Up event detected");
          }*/

          spans[0].onClick(widget);

          buffer.removeSpan(mGray);
        }

        return true;
      }
    } else {
      buffer.removeSpan(mGray);
    }

    return Touch.onTouchEvent(widget, buffer, event);
  }