下面列出了android.text.style.ClickableSpan#onClick() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean handleClickableSpanActionUiThread(
View view, int virtualDescendantId, Bundle arguments) {
Parcelable span = arguments.getParcelable(ACTION_ARGUMENT_ACCESSIBLE_CLICKABLE_SPAN);
if (!(span instanceof AccessibilityClickableSpan)) {
return false;
}
// Find the original ClickableSpan if it's still on the screen
AccessibilityNodeInfo infoWithSpan = null;
AccessibilityNodeProvider provider = view.getAccessibilityNodeProvider();
if (provider != null) {
infoWithSpan = provider.createAccessibilityNodeInfo(virtualDescendantId);
} else if (virtualDescendantId == AccessibilityNodeProvider.HOST_VIEW_ID) {
infoWithSpan = view.createAccessibilityNodeInfo();
}
if (infoWithSpan == null) {
return false;
}
// Click on the corresponding span
ClickableSpan clickableSpan = ((AccessibilityClickableSpan) span).findClickableSpan(
infoWithSpan.getOriginalText());
if (clickableSpan != null) {
clickableSpan.onClick(view);
return true;
}
return false;
}
@Test
public void clicking_the_span_trigger_the_callback() {
ClickableSpan clickableSpanEZ = ClickableSpanEZ.from(mockListener, FAKE_CONTENT);
clickableSpanEZ.onClick(null);
verify(mockListener, times(1)).onSpanClick(FAKE_CONTENT);
}
@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[] links = buffer.getSpans(off, off, ClickableSpan.class);
if (links.length != 0) {
ClickableSpan link = links[0];
if (action == MotionEvent.ACTION_UP) {
if (link instanceof TextLinkSpan) {
((TextLinkSpan) link).onClick(
widget, TextLinkSpan.INVOCATION_METHOD_TOUCH);
} else {
link.onClick(widget);
}
} else if (action == MotionEvent.ACTION_DOWN) {
if (widget.getContext().getApplicationInfo().targetSdkVersion
>= Build.VERSION_CODES.P) {
// Selection change will reposition the toolbar. Hide it for a few ms for a
// smoother transition.
widget.hideFloatingToolbar(HIDE_FLOATING_TOOLBAR_DELAY_MS);
}
Selection.setSelection(buffer,
buffer.getSpanStart(link),
buffer.getSpanEnd(link));
}
return true;
} else {
Selection.removeSelection(buffer);
}
}
return super.onTouchEvent(widget, buffer, event);
}
@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);
}
private boolean handleTouchForSpans(MotionEvent event, View view) {
final int action = event.getActionMasked();
if (action == ACTION_CANCEL) {
clearSelection();
resetLongClick();
return false;
}
if (action == ACTION_MOVE && !mLongClickActivated && mLongClickRunnable != null) {
trackLongClickBoundaryOnMove(event);
}
final boolean clickActivationAllowed = !mLongClickActivated;
if (action == ACTION_UP) {
resetLongClick();
}
final Rect bounds = getBounds();
if (!isWithinBounds(bounds, event)) {
return false;
}
final int x = (int) event.getX() - bounds.left;
final int y = (int) event.getY() - bounds.top;
ClickableSpan clickedSpan = getClickableSpanInCoords(x, y);
if (clickedSpan == null && mClickableSpanExpandedOffset > 0) {
clickedSpan = getClickableSpanInProximityToClick(x, y, mClickableSpanExpandedOffset);
}
if (clickedSpan == null) {
clearSelection();
return false;
}
if (action == ACTION_UP) {
clearSelection();
if (clickActivationAllowed
&& (mSpanListener == null || !mSpanListener.onClick(clickedSpan, view))) {
clickedSpan.onClick(view);
}
} else if (action == ACTION_DOWN) {
if (clickedSpan instanceof LongClickableSpan) {
registerForLongClick((LongClickableSpan) clickedSpan, view);
}
setSelection(clickedSpan);
}
return true;
}
@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) {
OpenUrlHelper.openUrl(mActivity, ((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);
}
@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);
}