android 實現超鏈接

四種方式實現鏈接。

1. autoLink

1. xml設置android:autoLink,包括以下參數,其中:all自動識別類

android:autoLink="all"

android:autoLink="none"

android:autoLink="web"

android:autoLink="email"

android:autoLink="phone"

android:autoLink="map"

2.代碼設置

tv1.setAutoLinkMask(Linkify.ALL);
tv1.setAutoLinkMask(Linkify.WEB_URLS);
tv1.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);
tv1.setAutoLinkMask(Linkify.PHONE_NUMBERS);
tv1.setAutoLinkMask(Linkify.MAP_ADDRESSES);

2. <a>標籤 + setMovementMethod()

將顯示內容寫到資源文件,一般爲String.xml中,並且用<a>標籤來聲明鏈接,然後 setMovementMethod()激活鏈接

3. Html.fromHtml()+ setMovementMethod()

 用Html類的fromHtml()方法格式化要放到TextView裏的文字。然後 setMovementMethod()激活鏈接

4. Spannable + setMovementMethod()

用Spannable或實現它的類,如SpannableString。與其他方法不同的是,Spannable對象可以爲個別字符設置鏈接(當然也可以爲個別字符設置顏色、字體等,實現某些字符高亮顯示的效果等)。然後 setMovementMethod()激活鏈

示例代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <!--autoLink="all"自動識別-->
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:autoLink="all"/>
 
    <!--<a></a>標籤-->
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="@string/str_link"/>
 
    <!--fromHtml()-->
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
 
    <!--Spannable-->
    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
 
</LinearLayout>

 

public class MainActivity extends BaseActivity{
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_m_m);
        //1
        //xml設置autoLink
        //autoLink="none" 普通文本
        //autoLink="phone" 電話
        //autoLink="all" 自動識別
        TextView tv1= (TextView) findViewById(R.id.tv1);
        //tv1.setText("百度:https://m.baidu.com/");
        //tv1.setText("電話:15555555555");
        tv1.setText("百度:https://m.baidu.com/");
        //代碼設置
        //tv1.setAutoLinkMask(Linkify.ALL);
        //tv1.setAutoLinkMask(Linkify.WEB_URLS);
        //tv1.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);
        //tv1.setAutoLinkMask(Linkify.PHONE_NUMBERS);
        //tv1.setAutoLinkMask(Linkify.MAP_ADDRESSES);
 

        //2. 將顯示內容寫到String.xml中
        TextView tv2= (TextView) findViewById(R.id.tv2);
        tv2.setMovementMethod(LinkMovementMethod.getInstance());//激活鏈接
 

        //3. 用Html類的fromHtml()方法
        TextView tv3= (TextView) findViewById(R.id.tv3);
        tv3.setText(Html.fromHtml(
                        "百度:" + "<a href='http://m.baidu.com'>點擊跳轉百度</a> ")
          );
        tv3.setMovementMethod(LinkMovementMethod.getInstance());//激活鏈接
 

        //4. Spannable
        TextView tv4= (TextView) findViewById(R.id.tv4);
        SpannableString ss = new SpannableString("百度: 點擊跳轉百度");
        ss.setSpan(new URLSpan("http://m.baidu.com"), 7, 18,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置4~18爲網站鏈接
        tv4.setText(ss);
        tv4.setMovementMethod(LinkMovementMethod.getInstance());//激活鏈接
    }
}

 

<string name="str_link">
    百度:<a href="http://m.baidu.com">點擊跳轉百度!</a>
</string>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章