Android 的实现TextView中文字链接的方式有很多种。
总结起来大概有4种:
1.当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是 android:autoLink="all"。当然也可以在java代码里做,textView01.setAutoLinkMask(Linkify.ALL);
2.将要处理的文字写到一个资源文件,如string.xml,然后的java代码里引用(直接写在代码了是不可行的,会直接把文字都显示处理)
3.用Html类的fromHtml()方法格式化要放到TextView里的文字
4.用Spannable或实现它的类,如SpannableString来格式,部分字符串。
当然以上各种方法,不要在java代码里加上textView03.setMovementMethod(LinkMovementMethod.getInstance());这样的代码,否则无效。
java代码
  1. importandroid.app.Activity;
  2. importandroid.graphics.Typeface;
  3. importandroid.os.Bundle;
  4. importandroid.text.Html;
  5. importandroid.text.SpannableString;
  6. importandroid.text.Spanned;
  7. importandroid.text.method.LinkMovementMethod;
  8. importandroid.text.method.ScrollingMovementMethod;
  9. importandroid.text.style.StyleSpan;
  10. importandroid.text.style.URLSpan;
  11. importandroid.text.util.Linkify;
  12. importandroid.widget.TextView;
  13. publicclassLinkTestextendsActivity{
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. TextViewtextView01=(TextView)findViewById(R.id.textView01);
  19. textView01.setAutoLinkMask(Linkify.ALL);
  20. StringautoLinkText="http://student.csdn.net/?232885我的CSDN博客";
  21. textView01.setText(autoLinkText);
  22. TextViewtextView02=(TextView)findViewById(R.id.textView02);
  23. //StringaLinkText="<ahref=\"http://student.csdn.net/?232885\">我的CSDN博客</a>"
  24. //+"<ahref=\"tel:4155551212\">andmyphonenumber</a>";
  25. //textView02.setText(aLinkText);
  26. textView02.setMovementMethod(LinkMovementMethod.getInstance());
  27. TextViewtextView03=(TextView)findViewById(R.id.textView03);
  28. StringhtmlLinkText="<ahref=\"http://student.csdn.net/?232885\"><u>我的CSDN博客</u></a>";
  29. textView03.setText(Html.fromHtml(htmlLinkText));
  30. textView03.setMovementMethod(LinkMovementMethod.getInstance());
  31. TextViewtextView04=(TextView)findViewById(R.id.textView04);
  32. SpannableStringss=newSpannableString("call:4155551212.");
  33. ss.setSpan(newStyleSpan(Typeface.BOLD),0,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  34. ss.setSpan(newURLSpan("tel:4155551212"),6,16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  35. textView04.setText(ss);
  36. textView04.setMovementMethod(LinkMovementMethod.getInstance());
  37. }
  38. }
string.xml文件
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,LinkTest!</string>
  4. <stringname="app_name">LinkTest</string>
  5. <stringname="aLinkText">
  6. <ahref="http://student.csdn.net/?232885">我的CSDN博客</a>
  7. </string>
  8. </resources>

main.xml文件
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:autoLink="all"
  7. >
  8. <TextView
  9. android:id="@+id/textView01"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. />
  13. <TextView
  14. android:id="@+id/textView02"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/aLinkText"
  18. />
  19. <TextView
  20. android:id="@+id/textView03"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. />
  24. <TextView
  25. android:id="@+id/textView04"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. />
  29. </LinearLayout>

更多相关文章

  1. Android的源代码结构(转)
  2. Android应用实例之----基于Service与ContentProvider的音乐播放
  3. Android获取其他包的Context实例
  4. Android(安卓)代码混淆
  5. Android(安卓)Jni代码示例讲解
  6. 如何在Android真机上检测是否有Google Map add-on
  7. Android下Activity全屏显示实现方法
  8. 利用HTML5开发Android
  9. Android高手进阶教程(七)之----Android(安卓)中Preferences的使

随机推荐

  1. Android的Layout整理
  2. winxp下j2me环境,android,nokiaSdk的配置
  3. maven管理android项目 环境搭建
  4. android和java中常见 Exception
  5. Android中的onWindowFocusChanged()方法
  6. 几个Android云测试
  7. Android 驱动之旅 (Based on Galaxy Nexu
  8. Android编程心得分享――JSON学习过程
  9. android ANR产生原因和解决办法
  10. LeakCanary使用详细教程(附Demo)