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

android:TextView中的文本链接之--链接的几种方式

2016-05-05 17:33 489 查看
http://2711082222.blog.163.com/blog/static/106302249201231145120562/

在TextView中使用超级连接有几种方式:

1.TextView设置:
autoLink:一共有几种值:web,phone, map, email, all, none.分别是url连接。电话号码提取拨号,地图地址。电子邮件,全部解释就是能支持的超级连接全部起作用,none就是默认情况,没有超链接。
android:autoLink="web"
//是将文本的web网址解释成超链接
textView01.setAutoLinkMask(Linkify.ALL);
2.setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。

3.小例子:
参考:ApiDemo 源码 com.example.android.apis.text.Link 类



1.//使用 android:autolink 属性自动链接

TextView textView01 = (TextView) findViewById(R.id.textView1);

2. //用在文本中使用 <a> 标签,如:

TextView textView02 = (TextView) findViewById(R.id.textView2);

textView02.setMovementMethod(LinkMovementMethod.getInstance());

3. //和上面一样, 只不过将文本改在 JAVA 代码中

TextView textView03 = (TextView) findViewById(R.id.textView3);

textView03.setText(

Html.fromHtml(

"<b>text3:</b> Text with a " +

"<a href=\"http://2711082222.blog.163.com\">link</a> " +

"created in the Java source code using HTML."));

textView03.setMovementMethod(LinkMovementMethod.getInstance());

4. //增加了颜色 设置 <font color> 标签

TextView textView04 = (TextView) findViewById(R.id.textView4);

textView04.setText( Html.fromHtml(

"<b>text4:</b> Text with a " +

"<a href=\"http://2711082222.blog.163.com\">link</a> " +

"created in the Java source code using HTML."

+"<font color='#00FF00'>" + "searchString" + "</FONT>" ));

textView04.setMovementMethod(LinkMovementMethod.getInstance());

5. //纯“手工”的了。通过创建 SpanableString 字符串,并在之上创 建一个或多个 Span 来实现丰富的效果。

TextView textView05 = (TextView) findViewById(R.id.textView5);

SpannableString ss = new SpannableString("call: 4155551212.");

ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

ss.setSpan(new URLSpan("tel:4155551212"), 6, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView05.setText(ss);

textView05.setMovementMethod(LinkMovementMethod.getInstance());

6. TextView t6 = (TextView) findViewById(R.id.textView6);

SpannableString tt = new SpannableString("text4: Click here to dial the phone.");

tt.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

tt.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

t6.setText(tt);

t6.setMovementMethod(LinkMovementMethod.getInstance());
----------------------
<!-- text1 automatically linkifies things like URLs and phone numbers. -->

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:autoLink="all"

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

<!-- text2 uses a string resource containing explicit <a> tags to

specify links. -->

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/link_text_manual"/>
<TextView

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>
<TextView

android:id="@+id/textView4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>
<TextView

android:id="@+id/textView5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>
<TextView

android:id="@+id/textView6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView" />
-----------------------------------
<string name="link_text_auto"><b>text1:</b> For instance,

you can click on http://2711082222.blog.163.com and it will launch the

web browser. You can click on google.com too. And, if you

click on (415) 555-1212 it should dial the phone.

</string>

<string name="link_text_manual"><b>text2:</b> <a href="http://2711082222.blog.163.com/">我的网易博客 </a>,

<a href="http://2711082222.blog.163.com/">link</a>,

<a> tag. Use a \"tel:\" URL to,

<a href="tel:4155551212">dial a phone number</a>.

</string>
----------------------
注意:
.setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。看个截图就更明白了:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: