android TextView(2)-根據類型超鏈接

autoLink屬性

當文字出現URL,EMail,Phone,Map的時候,可以通過設置autoLink屬性;這時當我們點擊文字對應部分文字的時候,即可跳轉至某默認APP.

<TextView
    android:id="@+id/link_tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:autoLink="phone"
    android:text="13843843888"/>

<TextView
    android:id="@+id/link_tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:autoLink="web"
    android:text="百度:www.baidu.com"/>


<TextView
    android:id="@+id/link_tv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:autoLink="all"
    android:text="13843843888    \n  百度:www.baidu.com    \n [email protected]"/>

all就是全部都包含,自動識別協議頭~ 在Java代碼中可以調用setAutoLinkMask(Linkify.ALL);
這個時候可以不寫協議頭,autolink會自動識別,但是還要爲這個TextView設置: setMovementMethod(LinkMovementMethod.getInstance()); 不然不會有鏈接效果

佈局文件

<TextView
    android:id="@+id/link_tv4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="13843843888    \n  百度:www.baidu.com    \n [email protected]"/>

java 代碼

mMy_Tv_Link4.setAutoLinkMask(Linkify.ALL);
mMy_Tv_Link4.setMovementMethod(LinkMovementMethod.getInstance());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章