TextView 中确定 ClickableSpan 的具体位置
来源:互联网 发布:洛杉矶之战 知乎 编辑:程序博客网 时间:2024/04/27 13:29
现在有一个需求是 TextView 中要有特殊的字符加颜色还需要可以点击弹出pop,点击跳转和改变颜色 这个需求 ClickableSpan 就可以胜任。但是找到具体位置就有点困难。
1.确定 ClickableSpan 的具体位置
注意 需要 加上 textView.setMovementMethod(LinkMovementMethod.getInstance());表示可以点击的
SpannableString ssb = new SpannableString("xxxxx");ssb.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { TextView parentTextView = (TextView) widget; Rect parentTextViewRect = new Rect(); // Initialize values for the computing of clickedText position SpannableString completeText = (SpannableString)(parentTextView).getText(); Layout textViewLayout = parentTextView.getLayout(); double startOffsetOfClickedText = completeText.getSpanStart(this); double endOffsetOfClickedText = completeText.getSpanEnd(this); double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)startOffsetOfClickedText); double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)endOffsetOfClickedText); // Get the rectangle of the clicked text int currentLineStartOffset = textViewLayout.getLineForOffset((int)startOffsetOfClickedText); int currentLineEndOffset = textViewLayout.getLineForOffset((int)endOffsetOfClickedText); boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset; textViewLayout.getLineBounds(currentLineStartOffset, parentTextViewRect); // Update the rectangle position to his real position on screen int[] parentTextViewLocation = {0,0}; parentTextView.getLocationOnScreen(parentTextViewLocation); double parentTextViewTopAndBottomOffset = ( parentTextViewLocation[1] - parentTextView.getScrollY() + parentTextView.getCompoundPaddingTop() ); parentTextViewRect.top += parentTextViewTopAndBottomOffset; parentTextViewRect.bottom += parentTextViewTopAndBottomOffset; parentTextViewRect.left += ( parentTextViewLocation[0] + startXCoordinatesOfClickedText + parentTextView.getCompoundPaddingLeft() - parentTextView.getScrollX() ); parentTextViewRect.right = (int) ( parentTextViewRect.left + endXCoordinatesOfClickedText - startXCoordinatesOfClickedText ); int x = (parentTextViewRect.left + parentTextViewRect.right) / 2; int y = parentTextViewRect.bottom; if (keywordIsInMultiLine) { x = parentTextViewRect.left; } spanTextLeft = x; spanTextTop = parentTextViewRect.top; spanTextBottom = y; ///显示弹出层 showSubTitlePop(widget,name,stockid.toUpperCase()); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(xxxx); // 设置文本颜色 // 去掉下划线 ds.setUnderlineText(false); } }, start, start + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
注意点:
1.double startOffsetOfClickedText = completeText.getSpanStart(this);
这个this是 ClickableSpan onclick 里面的this,其他的 this 是获取不到具体的值得。
2. x,y 就是最终 左边距和上边距,获取的是全屏的具体距离不是相对 textView 的
2.参考资料
How get coordinate of a ClickableSpan inside a TextView?
http://stackoverflow.com/questions/11905486/how-get-coordinate-of-a-clickablespan-inside-a-textview
0 0
- TextView 中确定 ClickableSpan 的具体位置
- TextView ClickableSpan 事件分发的两个坑
- TextView ClickableSpan 事件分发的两个坑
- 在TextView中使用了ClickableSpan后,禁止TextView滑动。
- TextView中ClickableSpan与文本自由复制(TextIsSelectable)冲突问题
- android TextView ClickableSpan 长按报错
- TextView添加ClickableSpan和LinkMovementMethod之间的关系
- TextView添加ClickableSpan和LinkMovementMethod之间的关系
- linux内核中默认logo的具体位置
- android TextView中ClickableSpan与文本自由复制(TextIsSelectable)冲突问题
- android中TextView 添加ClickableSpan后点击选中文字背景问题
- android中TextView 添加ClickableSpan后点击选中文字背景问题
- NGUI中计算UILabel中每个文字的具体位置
- ListView的Item里的TextView设置ClickableSpan和LinkMovementMethod导致ListView无法响应点击事件
- ListView里TextView使用ClickableSpan,ListView的Item无法点击的解决办法
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
- Android TextView 自定义ClickableSpan实现@ 超链接
- 数据结构——树的存储结构孩子表示法
- 实现一个简单的压测工具
- php持久登录
- 《SSH框架》---还你一个清晰的配置
- hiho1487岛屿3
- TextView 中确定 ClickableSpan 的具体位置
- 高性能Mysql主从架构的复制原理及配置详解
- 基于hadoop-2.6.0的hbase完全分布式安装
- Angular 2中覆盖子组件的样式
- 三角形的周长和面积
- 字符编码
- slam学习(5)-vo (visual odometry)视觉里程计和图优化工具g2o
- Activity的四种启动模式
- 【Leetcode】507. Perfect Number