Clickable URLs in Android TextViews
来源:互联网 发布:caffe入门教程用什么 编辑:程序博客网 时间:2024/06/01 01:33
http://blog.elsdoerfer.name/2009/10/29/clickable-urls-in-android-textviews/
Android’s TextView widget can contain clickable URLs. It can easily make web addresses open in the browser, or connect phone numbers with the dialer. All that is amazing compared to the last GUI framework I used, Delphi’s once greatVCL).
Unfortunately, both TextView and the Linkify utility it uses basically hardcode URL click handling to Intents, by way of theURLSpans they create. What if we want the link to affect something within your own Activity, say, display a dialog, or enable a filter?
For example, in Autostarts, if the user’s filters cause the application list to be empty, I wanted to display an explanatory message, and provide a quick and easy way for the user to rectify the situation, i.e. lead him towards the filter selection dialog. Making the whole text clickable is hard to get right visually, and I didn’t like the idea of a button too much. A link within the text seemed perfect.
Now, we could just use a custom URL scheme of course, and register our Activity to handle Intents for that scheme, but that seemed much too heavy, if not hacky. Why shouldn’t we be able to just hook up an onClick handler?
As mentioned, URLSpan doesn’t allow us to change the way it handles clicks (it always sends off an Intent), but we can create a subclass:
- static class InternalURLSpan extends ClickableSpan {
- OnClickListener mListener;
- public InternalURLSpan(OnClickListener listener) {
- mListener = listener;
- }
- @Override
- public void onClick(View widget) {
- mListener.onClick(widget);
- }
- }
That looks pretty decent. Actually using that class it is tougher. There is no way to tellTextView or Linkify to use our custom span. In fact, Linkify actually hasa method (applyLink) that would be nearly perfect to override, but declares it final.
So, we end up having to generate the spans manually; note nice, but hey, it works.
- SpannableString f = new SpannableString("....")
- f.setSpan(new InternalURLSpan(new OnClickListener() {
- public void onClick(View v) {
- showDialog(DIALOG_VIEW_OPTIONS);
- }
- }), x, y, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
We probably also want the user to jump to your link my moving the focus (e.g. using the trackball), which we can do by setting the proper movement method:
- MovementMethod m = emptyText.getMovementMethod();
- if ((m == null) || !(m instanceof LinkMovementMethod)) {
- if (textView.getLinksClickable()) {
- textView.setMovementMethod(LinkMovementMethod.getInstance());
- }
- }
- Clickable URLs in Android TextViews
- Android事件系统(2)ClickableSpan In Clickable TextView
- Android--- android:clickable属性
- android:clickable 问题
- Friendly URLs in Tapestry
- Friendly URLs in Tapestry
- Friendly URLs in Tapestry
- android问题1、clickable问题
- Enable Clean URLs in Drupal
- URLs
- URLS
- URLs
- URLs
- URLs
- URLs
- urls
- URLs
- Android Intent Scheme URLs攻击
- 又见sql注入
- "黑马程序员"-到底重载了哪个方法
- 详解C/C++预处理器
- android中用Spannable在TextView中设置超链接、颜色、字体
- Wince学习资料推荐
- Clickable URLs in Android TextViews
- 可渲染样条线
- win7、fedora 双系统 安装及引导修复
- slab分配机制
- 图的邻接表算法
- 华为,我们的光环还能顶多久
- TCP/IP网络是如何通讯的
- 简单的数组类 Array
- linux模块开发必要的基础知识