转:android TextView中超链接的事件捕捉(textview上LINK的点击事件)

Java代码 复制代码 收藏代码

  1. package com.cm;    
  2. import java.util.ArrayList;    
  3. import android.R.color;    
  4. import android.app.Activity;    
  5. import android.content.Context;    
  6. import android.graphics.Color;    
  7. import android.os.Bundle;    
  8. import android.text.Html;    
  9. import android.text.Spannable;    
  10. import android.text.SpannableStringBuilder;    
  11. import android.text.method.LinkMovementMethod;    
  12. import android.text.style.ClickableSpan;    
  13. import android.text.style.URLSpan;    
  14. import android.view.View;    
  15. import android.widget.TextView;    
  16. import android.widget.Toast;    
  17. public class TextHtml extends Activity {    
  18. private TextView tv;    
  19. private static Context ctx;    
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {    
  22. super.onCreate(savedInstanceState);    
  23.         tv = new TextView(this);    
  24.         ctx = this;    
  25.         String htmlLinkText =  "我是超链接"+ "<a style=\"color:red;\" href='lianjie'>超链接点击事件</a>";    
  26. // 文字的样式(style)被覆盖,不能改变……  
  27.         tv.setText(Html.fromHtml(htmlLinkText));    
  28.         tv.setMovementMethod(LinkMovementMethod.getInstance());    
  29.         CharSequence text = tv.getText();    
  30. if (text instanceof Spannable) {    
  31. int end = text.length();    
  32.             Spannable sp = (Spannable) tv.getText();    
  33.             URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);    
  34.             SpannableStringBuilder style = new SpannableStringBuilder(text);    
  35.             style.clearSpans();// should clear old spans  
  36. //循环把链接发过去            
  37. for (URLSpan url : urls) {    
  38.                 MyURLSpan myURLSpan = new MyURLSpan(url.getURL());    
  39.                 style.setSpan(myURLSpan, sp.getSpanStart(url),    
  40.                         sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
  41.             }    
  42.             tv.setText(style);    
  43.         }    
  44.         setContentView(tv);    
  45.     }    
  46. private static class MyURLSpan extends ClickableSpan {    
  47. private String mUrl;    
  48.         MyURLSpan(String url) {    
  49.             mUrl = url;    
  50.         }    
  51. @Override
  52. public void onClick(View widget) {    
  53. if(mUrl .equal(lianjie)){    
  54.                 Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show();    
  55.                 widget.setBackgroundColor(Color.parseColor("#00000000"));    
  56.             }    
  57.         }    
  58.     }    
  59. }   
package com.cm; import java.util.ArrayList; import android.R.color; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.URLSpan; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class TextHtml extends Activity { private TextView tv; private static Context ctx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv = new TextView(this); ctx = this; String htmlLinkText = "我是超链接"+ "<a style=\"color:red;\" href='lianjie'>超链接点击事件</a>"; // 文字的样式(style)被覆盖,不能改变…… tv.setText(Html.fromHtml(htmlLinkText)); tv.setMovementMethod(LinkMovementMethod.getInstance()); CharSequence text = tv.getText(); if (text instanceof Spannable) { int end = text.length(); Spannable sp = (Spannable) tv.getText(); URLSpan[] urls = sp.getSpans(0, end, URLSpan.class); SpannableStringBuilder style = new SpannableStringBuilder(text); style.clearSpans();// should clear old spans //循环把链接发过去 for (URLSpan url : urls) { MyURLSpan myURLSpan = new MyURLSpan(url.getURL()); style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); } tv.setText(style); } setContentView(tv); } private static class MyURLSpan extends ClickableSpan { private String mUrl; MyURLSpan(String url) { mUrl = url; } @Override public void onClick(View widget) { if(mUrl .equal(lianjie)){ Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show(); widget.setBackgroundColor(Color.parseColor("#00000000")); } } } }

你可能感兴趣的:(java,android,超链接,target,blank)