您的位置:首页 > 移动开发 > Android开发

Android TextView 添加链接

2012-10-17 09:06 274 查看
使用autoLink,只要用这个属性,我们textview就可以添加链接了。

当我们在代码中写成android:autoLink="all"我们就可以在textview里面写的文字中包含网址、电话、email的会自动加入连接地址。

Java代码:
package eoe.link;

<TextView 

xmlns:android="http://schemas.android.com/apk/res/android

android:id="@+id/text1" 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

android:autoLink="all" 

android:text="@string/link_text_auto" />

  使用一个字符串资源含直接<一个>标签来说明链接。

Java代码:

  packageeoe.link;

  

  

  <stringname="link_text_manual">

  <b>text2:</b>Thisissomeother

  text,witha<ahref="http://www.google.com">link</a>specified

  viaan<a>tag.Usea\"tel:\"URL

  to<ahref="tel:4155551212">dialaphonenumber</a>.

  </string>

  

  TextViewt2=(TextView)findViewById(R.id.text2);

  t2.setMovementMethod(LinkMovementMethod.getInstance());

       建立了文本中使用HTML代码。

Java代码:

package eoe.link;

TextView t3 = (TextView) findViewById(R.id.text3); 

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."));

t3.setMovementMethod(LinkMovementMethod.getInstance());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐