首先看一个链接文本执行流程的大概示意图
看了这个流程应该对大概URL, Email等的链接实现有所了解了,所以直接看代码把
XML布局文件
默认的链接在点击时,只会跳转到响应的View,如点击URL时,打开浏览器,点击电话号码时,打开拨号界面。 如果我们要在点击某连接时,跳转到某个Activity呢。
URLSpan继承自ClickableSpan,所以我们的出发点也是这个类,然后重写其中的onClick
使用的话和URLSpan差不多
看了这个流程应该对大概URL, Email等的链接实现有所了解了,所以直接看代码把
- TextViewview1=(TextView)findViewById(R.id.view1);
- TextViewview2=(TextView)findViewById(R.id.view2);
- TextViewview3=(TextView)findViewById(R.id.view3);
- TextViewview4=(TextView)findViewById(R.id.view4);
- TextViewview5=(TextView)findViewById(R.id.view5);
- TextViewview6=(TextView)findViewById(R.id.view6);
- SpannableStringtext1=newSpannableString("red");
- text1.setSpan(newForegroundColorSpan(Color.RED),0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view1.setText(text1);
- SpannableStringtext2=newSpannableString("green");
- text2.setSpan(newBackgroundColorSpan(Color.GREEN),0,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view2.setText(text2);
- SpannableStringtext3=newSpannableString("iambold");
- text3.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),5,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view3.setText(text3);
- SpannableStringtext4=newSpannableString("underline");
- text4.setSpan(newUnderlineSpan(),0,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view4.setText(text4);
- SpannableStringtext5=newSpannableString("deleteline");
- text5.setSpan(newStrikethroughSpan(),7,11,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view5.setText(text5);
- SpannableStringtext6=newSpannableString("makeacall");
- text6.setSpan(newURLSpan("tel:123456789"),7,11,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view6.setText(text6);
- view6.setMovementMethod(LinkMovementMethod.getInstance());
- //还有ImageSpan等
XML布局文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schem<wbr>as.android.c<wbr>om/apk/res/a<wbr>ndroid</wbr></wbr></wbr>"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/view1"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/view2"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/view3"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/view4"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/view5"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/view6"
- android:layout_width="wrap_content"
- android:layout_height="50sp"
- android:gravity="center"
- />
- </LinearLayout>
默认的链接在点击时,只会跳转到响应的View,如点击URL时,打开浏览器,点击电话号码时,打开拨号界面。 如果我们要在点击某连接时,跳转到某个Activity呢。
URLSpan继承自ClickableSpan,所以我们的出发点也是这个类,然后重写其中的onClick
- publicclassIntentSpanextendsClickableSpanimplementsParcelableSpan{
- privateIntentmIntent;
- publicIntentSpan(IntenttoActivity){
- mIntent=toActivity;
- }
- @Override
- publicvoidonClick(ViewsourceView){
- Contextcontext=sourceView.getContext();
- context.startActivity(mIntent);
- }
- @Override
- publicintgetSpanTypeId(){
- return100;
- }
- @Override
- publicintdescribeContents(){
- return0;
- }
- @Override
- publicvoidwriteToParcel(Parceldest,intflag){
- //don'twritetoparcel
- }
- publicIntentgetIntent(){
- returnmIntent;
- }
- }
使用的话和URLSpan差不多
- SpannableStringtext6=newSpannableString("makeacall");
- Intentintent=newIntent(context,SomActivity.class);
- text6.setSpan(newIntentSpan(intent),7,11,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- view6.setText(text6);
- view6.setMovementMethod(LinkMovementMethod.getInstance());