1、使用android:autoLink="all"只需在TextView中加入这个属性,而在TextView里面写的文字中包含网址、电话、email的会自动加入连接地址。
如:
<TextViewxmlns: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"
/>
2、使用<stringname=””><ahref=””></a></string>标签,建立超链接:
如:
<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());
3、在java文件中使用HTML语言:
如:
TextViewt3=(TextView)findViewById(R.id.text3);
t3.setText(Html.fromHtml("<b>text3:</b>Textwitha"
+"<ahref=\"http://www.google.com\">link</a>"
+"createdintheJavasourcecodeusingHTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
4、字符串截取方法
SpannableStringss=newSpannableString("text4:Clickheretodialthephone.");
ss.setSpan(newStyleSpan(Typeface.BOLD),0,6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(
newURLSpan("tel:4155551212"),13,17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextViewt4=(TextView)findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
5、Android中我们为了实现文本的滚动可以在ScrollView中嵌入一个TextView,其实TextView自己也可以实现多行滚动的,毕竟ScrollView必须只能有一个直接的子类布局。只要在layout中简单设置几个属性就可以轻松实现
<TextView
android:id="@+id/tvCWJ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"<!--垂直滚动条-->
android:singleLine="false"<!--实现多行-->
android:maxLines="15"<!--最多不超过15行-->
android:textColor="#FF0000"
/>
当然我们为了让TextView动起来,还需要用到TextView的setMovementMethod方法设置一个滚动实例,代码如下
TextViewtvAndroid123=(TextView)findViewById(R.id.tvCWJ);
tvAndroid123.setMovementMethod(ScrollingMovementMethod.getInstance());
ad_link=(TextView)findViewById(R.id.ad_link);
ad_link.setText(Html.fromHtml("<ahref="\"mce_href="\"""+mURL.getLink()+"\">"+Html.fromHtml(mURL.getLabel()+"</a>")));
ad_link.setMovementMethod(LinkMovementMethod.getInstance());