public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if(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);
mClickLinks = buffer.getSpans(off, off, ClickableSpan.class);
if(mClickLinks.length > 0){
// 点击的是Span区域,不要把点击事件传递
setPassToTv(false);
Selection.setSelection(buffer,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]));
//设置点击区域的背景色
mBgSpan = new BackgroundColorSpan(mPressSpanBgColor);
buffer.setSpan(mBgSpan,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
setPassToTv(true);
// textView选中效果
widget.setBackgroundColor(mPressSpanBgColor);
}
}else if(action == MotionEvent.ACTION_UP){
if(mClickLinks.length > 0){
mClickLinks[0].onClick(widget);
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
}
Selection.removeSelection(buffer);
widget.setBackgroundColor(mTextViewBgColor);
}else if(action == MotionEvent.ACTION_MOVE){
//这种情况不用做处理
}else{
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
widget.setBackgroundColor(mTextViewBgColor);
}
return Touch.onTouchEvent(widget, buffer, event);
}
TouchMovementMethod.java 文件源码
java
阅读 16
收藏 0
点赞 0
评论 0
项目:CommentView
作者:
评论列表
文章目录