Android TextView文字链接4中方法

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.youkuaiyun.com/?232885我的优快云博客";
  21. textView01.setText(autoLinkText);
  22. TextViewtextView02=(TextView)findViewById(R.id.textView02);
  23. //StringaLinkText="<ahref=\"http://student.youkuaiyun.com/?232885\">我的优快云博客</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.youkuaiyun.com/?232885\"><u>我的优快云博客</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.youkuaiyun.com/?232885">我的优快云博客</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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值