TextView中的链接

来源:互联网 发布:qq聊天室软件 编辑:程序博客网 时间:2024/04/29 13:38
从网上转来的,记录一下,稍后有时间再整理。

android textView 加入超链接方式:

1:使用android:autoLink="all" 只需在textview中加入这个属性 在里面写的文字中包含网址、电话、email的会自动加入连接地址。 
如:
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
  2. android:id="@+id/text1" android:layout_width="match_parent" 
  3. android:layout_height="match_parent" android:autoLink="all" 
  4. android:text="@string/link_text_auto" /> 
复制代码
2:uses a string resource containing explicit <a> tags to specify links. 
如:
  1.     <string name="link_text_manual"><b>text2:</b> This is some other text, with a <a href="http://www.google.com">link</a> specified via an &lt;a&gt; tag.  Use a \"tel:\" URL to <a href="tel:4155551212">dial a phone number</a>.</string> 
复制代码
别忘了 
TextView t2 = (TextView) findViewById(R.id.text2); 
t2.setMovementMethod(LinkMovementMethod.getInstance()); 
    
3: builds the text in the Java code using HTML
  1. TextView t3 = (TextView) findViewById(R.id.text3); 
  2. t3.setText(Html.fromHtml("<b>text3:</b>  Text with a " + "<a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML.")); 
  3. t3.setMovementMethod(LinkMovementMethod.getInstance()); 
复制代码
4:字符串截取方法
  1. SpannableString ss = new SpannableString("text4: Click here to dial the phone."); 
  2. ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
  3. ss.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

  4. TextView t4 = (TextView) findViewById(R.id.text4); 
  5. t4.setText(ss); 
  6. t4.setMovementMethod(LinkMovementMethod.getInstance()); 
复制代码
0 0