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 great VCL).

Unfortunately, both TextView and the Linkify utility it uses basically hardcode URL click handling to Intents, by way of the URLSpans 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:

view plain copy to clipboard print ?
  1. staticclassInternalURLSpanextendsClickableSpan{
  2. OnClickListenermListener;
  3. publicInternalURLSpan(OnClickListenerlistener){
  4. mListener=listener;
  5. }
  6. @Override
  7. publicvoidonClick(Viewwidget){
  8. mListener.onClick(widget);
  9. }
  10. }

That looks pretty decent. Actually using that class it is tougher. There is no way to tell TextView or Linkify to use our custom span. In fact, Linkify actually has a 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.

view plain copy to clipboard print ?
  1. SpannableStringf=newSpannableString("....")
  2. f.setSpan(newInternalURLSpan(newOnClickListener(){
  3. publicvoidonClick(Viewv){
  4. showDialog(DIALOG_VIEW_OPTIONS);
  5. }
  6. }),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:

view plain copy to clipboard print ?
  1. MovementMethodm=emptyText.getMovementMethod();
  2. if((m==null)||!(minstanceofLinkMovementMethod)){
  3. if(textView.getLinksClickable()){
  4. textView.setMovementMethod(LinkMovementMethod.getInstance());
  5. }
  6. }

This entry was posted by admin on 05:28, Oct 29th 2009 and filed in Android.

You can subscribe the RSS Feed, leave a reply or trackback from your own blog.




----------------------------------------------------------------------------------

Why did you have to create a custom subclass of ClickableSpan. Instead you can create a ClickableSpan variable:

ClickableSpan clickable = new ClickableSpan() {@Overridepublic void onClick(View widget) {            Log.e("select text", "hi");}};SpannableString f = new SpannableString("....") ;f.setSpan(clickable,  x, y, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android manifest属性
  2. 最新Android ADT, SDK, SDK_tool等官方下
  3. 基本控件学习以( RadioGroup和RadioButton
  4. Android抽屉式按钮实现
  5. android sdk更新失败解决办法
  6. 【30篇突击 android】源码统计二
  7. Android ProgressDialog的使用
  8. 《Android程序运行过程,Android》
  9. Android资料
  10. Android SDK Manager无法自动更新