Java Code Examples for android.widget.TextView#getScrollX()

The following examples show how to use android.widget.TextView#getScrollX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkTouchMovementMethod.java    From materialup with Apache License 2.0 6 votes vote down vote up
private TouchableUrlSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent
        event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

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

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

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

    TouchableUrlSpan[] link = spannable.getSpans(off, off, TouchableUrlSpan.class);
    TouchableUrlSpan touchedSpan = null;
    if (link.length > 0) {
        touchedSpan = link[0];
    }
    return touchedSpan;
}
 
Example 2
Source File: TextViewClickMovement.java    From NMSAlphabetAndroidApp with MIT License 6 votes vote down vote up
private String getLinkText(final TextView widget, final Spannable buffer, final MotionEvent event) {
    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) {
        return buffer.subSequence(buffer.getSpanStart(link[0]),
                buffer.getSpanEnd(link[0])).toString();
    }

    return "";
}
 
Example 3
Source File: TextViewLinkHandler.java    From ResearchStack with Apache License 2.0 6 votes vote down vote up
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_UP) {
        return super.onTouchEvent(widget, buffer, event);
    }

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

    URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
    if (link.length != 0) {
        onLinkClick(link[0].getURL());
    }
    return true;
}
 
Example 4
Source File: LinkTextView.java    From LinkTextView with MIT License 6 votes vote down vote up
private TouchableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {

            int x = (int) event.getX();
            int y = (int) event.getY();

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

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

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

            TouchableSpan[] link = spannable.getSpans(off, off, TouchableSpan.class);
            TouchableSpan touchedSpan = null;
            if (link.length > 0) {
                touchedSpan = link[0];
            }
            return touchedSpan;
        }
 
Example 5
Source File: CustomLinkMovementMethod.java    From SimplifySpan with MIT License 6 votes vote down vote up
private CustomClickableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

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

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

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

    CustomClickableSpan[] link = spannable.getSpans(off, off, CustomClickableSpan.class);
    CustomClickableSpan touchedSpan = null;
    if (link.length > 0) {
        touchedSpan = link[0];
    }
    return touchedSpan;
}
 
Example 6
Source File: ClickableMovementMethod.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    // Just copied from android.text.method.LinkMovementMethod
    if (event.getAction() == MotionEvent.ACTION_UP) {
        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) {
            link[0].onClick(widget);
            return true;
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
 
Example 7
Source File: EmojiconTextView.java    From imsdk-android with MIT License 5 votes vote down vote up
private boolean handleLinkMovementMethod(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 (action == MotionEvent.ACTION_UP) {
                    long actionUpTime = System.currentTimeMillis();
                    if (actionUpTime - mLastActionDownTime > ViewConfiguration.getLongPressTimeout()) { //长按事件,取消LinkMovementMethod处理,即不处理ClickableSpan点击事件
                        return false;

                    }
                    link[0].onClick(widget);
                    Selection.removeSelection(buffer);

                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                    mLastActionDownTime = System.currentTimeMillis();
                }
            }
        }
        return false;
    }
 
Example 8
Source File: AccurateMovementMethod.java    From social-text-view with MIT License 5 votes vote down vote up
/**
 * Gets the span that was touched.
 * @param tv {@link TextView}
 * @param span {@link Spannable}
 * @param e {@link MotionEvent}
 * @return {@link TouchableSpan}
 */
private TouchableSpan getTouchedSpan(TextView tv, Spannable span, MotionEvent e) {
    // Find the location in which the touch was made
    int x = (int)e.getX();
    int y = (int)e.getY();

    // Ignore padding
    x -= tv.getTotalPaddingLeft();
    y -= tv.getTotalPaddingTop();

    // Account for scrollable text
    x += tv.getScrollX();
    y += tv.getScrollY();

    final Layout layout = tv.getLayout();
    final int touchedLine = layout.getLineForVertical(y);
    final int touchOffset = layout.getOffsetForHorizontal(touchedLine, x);

    // Set bounds of the touched line
    touchBounds.left = layout.getLineLeft(touchedLine);
    touchBounds.top = layout.getLineTop(touchedLine);
    touchBounds.right = layout.getLineRight(touchedLine);
    touchBounds.bottom = layout.getLineBottom(touchedLine);

    // Ensure the span falls within the bounds of the touch
    TouchableSpan touchSpan = null;
    if (touchBounds.contains(x, y)) {
        // Find clickable spans that lie under the touched area
        TouchableSpan[] spans = span.getSpans(touchOffset, touchOffset, TouchableSpan.class);
        touchSpan = (spans.length > 0) ? spans[0] : null;
    }

    return touchSpan;
}
 
Example 9
Source File: WordExtractor.java    From TextFiction with Apache License 2.0 5 votes vote down vote up
private float convertToLocalHorizontalCoordinate(TextView tv, float x) {
	x -= tv.getTotalPaddingLeft();
	// Clamp the position to inside of the view.
	x = Math.max(0.0f, x);
	x = Math.min(tv.getWidth() - tv.getTotalPaddingRight() - 1, x);
	x += tv.getScrollX();
	return x;
}
 
Example 10
Source File: ClickLinkMovementMethod.java    From xifan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

    int action = event.getActionMasked();
    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 (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else {
                Selection.setSelection(buffer, buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }
            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return false;
}
 
Example 11
Source File: LocalLinkMovementMethod.java    From JianshuApp with GNU General Public License v3.0 5 votes vote down vote up
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_DOWN) {
        return Touch.onTouchEvent(widget, buffer, event);
    }

    int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
    int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
    Layout layout = widget.getLayout();
    int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
    ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
    if (link.length != 0) {
        if (action == MotionEvent.ACTION_UP) {
            link[0].onClick(widget);
        } else {
            Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
        }
        if (!(widget instanceof TextViewFixTouchConsume)) {
            return true;
        }
        ((TextViewFixTouchConsume) widget).linkHit = true;
        return true;
    }
    Selection.removeSelection(buffer);
    Touch.onTouchEvent(widget, buffer, event);
    return false;
}
 
Example 12
Source File: BaseMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a scroll to line start action.
 * Scrolls to the start of the line.
 *
 * @param widget The text view.
 * @param buffer The text buffer.
 * @return True if the event was handled.
 * @hide
 */
protected boolean scrollLineStart(TextView widget, Spannable buffer) {
    final int minScrollX = getScrollBoundsLeft(widget);
    int scrollX = widget.getScrollX();
    if (scrollX > minScrollX) {
        widget.scrollTo(minScrollX, widget.getScrollY());
        return true;
    }
    return false;
}
 
Example 13
Source File: CustomMovementMethod.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
@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 (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }
            return true;
        }
    }

    return Touch.onTouchEvent(widget, buffer, event);
}
 
Example 14
Source File: LinkMovementMethod2.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
@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 (action == MotionEvent.ACTION_UP) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    UrlOpener.openUrl(widget.getContext(), ((URLSpan) span).getURL(), true);
                } else {
                    span.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}
 
Example 15
Source File: HackyMovementMethod.java    From BlackLight with GNU General Public License v3.0 4 votes vote down vote up
@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);
}
 
Example 16
Source File: LongClickableLinkMovementMethod.java    From RichText with MIT License 4 votes vote down vote up
@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);

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

        if (link.length != 0) {
            long currTime = System.currentTimeMillis();
            LongClickableSpan l = link[0];
            int ls = buffer.getSpanStart(l);
            int le = buffer.getSpanEnd(l);
            // 判断点击的点是否在Image范围内
            ClickableImageSpan[] is = buffer.getSpans(ls, le, ClickableImageSpan.class);
            if (is.length > 0) {
                if (!is[0].clicked(x)) {
                    Selection.removeSelection(buffer);
                    return false;
                }
            } else if (off < layout.getOffsetToLeftOf(ls) || off > layout.getOffsetToLeftOf(le + 1)) {
                // 判断点击位置是否在链接范围内
                Selection.removeSelection(buffer);
                return false;
            }

            if (action == MotionEvent.ACTION_UP) {
                // 如果按下时间超过500毫秒,触发长按事件
                if (currTime - lastTime > MIN_INTERVAL) {
                    if (!l.onLongClick(widget)) {
                        // onLongClick返回false代表事件未处理,交由onClick处理
                        l.onClick(widget);
                    }
                } else {
                    l.onClick(widget);
                }
            } else {
                Selection.setSelection(buffer,
                        ls, le);
            }
            lastTime = currTime;
            return true;
        } else {
            Selection.removeSelection(buffer);
            return false;
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}
 
Example 17
Source File: TextViewFixTouchConsume.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
@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 (action == MotionEvent.ACTION_UP) {
				link[0].onClick(widget);
			} else if (action == MotionEvent.ACTION_DOWN) {
				Selection.setSelection(buffer,
						buffer.getSpanStart(link[0]),
						buffer.getSpanEnd(link[0]));
			}

			if (widget instanceof TextViewFixTouchConsume) {
				((TextViewFixTouchConsume) widget).linkHit = true;
			}
			return true;
		} else {
			Selection.removeSelection(buffer);
			Touch.onTouchEvent(widget, buffer, event);
			return false;
		}
	}
	return Touch.onTouchEvent(widget, buffer, event);
}
 
Example 18
Source File: LongClickLinkMovementMethod.java    From mimi-reader with Apache License 2.0 4 votes vote down vote up
@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();
            lastRawX = (int) event.getRawX();
            lastRawY = (int) event.getRawY();

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

            LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);
//            SpoilerSpan[] check = buffer.getSpans(off, off, SpoilerSpan.class);

            if (link.length != 0) {
                // If the user is pressing down and there is no thread, make one and start it
                if (event.getAction() == MotionEvent.ACTION_DOWN && longClickSensor == null) {
                    thisLink = link[0];
                    lastWidget = widget;
                    longClickSensor = new java.lang.Thread(new MyDelayedAction());
                    longClickSensor.start();
                }
                // If the user has let go and there was a thread, stop it and forget about the thread
                if (event.getAction() == MotionEvent.ACTION_UP && longClickSensor != null) {
                    longClickSensor.interrupt();
                    longClickSensor = null;
                }
            }

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    if (System.currentTimeMillis() - lastClickTime < 800) {
                        link[0].onClick(widget);
                    }
                } else if (action == MotionEvent.ACTION_DOWN) {
                    // Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                    lastClickTime = System.currentTimeMillis();
                }
                return true;
            }
        } else {
            int deltaX = Math.abs((int) event.getRawX() - lastRawX);
            int deltaY = Math.abs((int) event.getRawY() - lastRawY);
            // must hold finger still
            if ((longClickSensor != null) && ((deltaX > 10) || (deltaY > 10))) {
                longClickSensor.interrupt();
                longClickSensor = null;
            }
        }

        return super.onTouchEvent(widget, buffer, event);
    }
 
Example 19
Source File: CustomMoveMethod.java    From TestChat with Apache License 2.0 4 votes vote down vote up
@Override
        public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                        case MotionEvent.ACTION_DOWN:
//                                                这里得到的x ,  y值只是相对于屏幕的位置
                                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 location = layout.getOffsetForHorizontal(layout.getLineForVertical(y), x);
                                clickableSpans = text.getSpans(location, location, ClickableSpan.class);
                                if (clickableSpans.length > 0) {
                                        isClickTextView = false;
                                        LogUtil.e("点击clickSpan位置");
//                                                        选中点击位置
//                                        Selection.setSelection(text, text.getSpanStart(clickableSpans[0]), text.getSpanEnd(clickableSpans[0]));
                                        text.setSpan(colorSpan = new BackgroundColorSpan(selectedColor), text.getSpanStart(clickableSpans[0]), text.getSpanEnd(clickableSpans[0]), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                        return true;
                                } else {
                                        isClickTextView = true;
                                        widget.setBackgroundColor(Color.parseColor("#979595"));
                                        LogUtil.e("点击的是正常textView的文字");
                                }
                                break;
                        case MotionEvent.ACTION_MOVE:
                                break;
                        case MotionEvent.ACTION_UP:
                                if (clickableSpans.length > 0) {
                                        LogUtil.e("点击span的up11");
//                                                        设置点击时间
                                        clickableSpans[0].onClick(widget);
                                        text.removeSpan(colorSpan);
//                                        Selection.removeSelection(text);
                                        return true;
                                } else {
                                        LogUtil.e("点击textView文字的up11");
                                        widget.setBackgroundColor(Color.parseColor("#00000000"));
                                }
                                break;
                        default:
                                if (colorSpan != null) {
                                        text.removeSpan(colorSpan);
//                                        Selection.removeSelection(text);
                                        return true;
                                } else {
                                        widget.setBackgroundColor(Color.parseColor("#00000000"));
                                }
                                break;
                }
                return super.onTouchEvent(widget, text, event);
        }
 
Example 20
Source File: LinkMovementMethod2.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@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 (action == MotionEvent.ACTION_UP) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    UrlOpener.openUrl(widget.getContext(), ((URLSpan) span).getURL(), true);
                } else {
                    span.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

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