textView 添加超鏈接(兩種實(shí)現(xiàn)方式)
更新時間:2013年06月08日 15:29:24 作者:
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動識別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動識別超鏈接。
代碼如下:
第一種
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = "有問題:\n";
html+="<a ;//注意這里必須加上協(xié)議號,即http://。
//否則,系統(tǒng)會以為該鏈接是activity,而實(shí)際這個activity不存在,程序就崩潰。
CharSequence charSequence = Html.fromHtml(html);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
第二種
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = "有問題:\n";
html+="www.baidu.com";//這里即使不加協(xié)議好HTTP;也能自動被系統(tǒng)識別出來。
textView.setText(html);
textView.setAutoLinkMask(Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
總結(jié)一下就是,以html顯示超鏈接,必須寫全url。以setAutoLinkMask(Linkify.ALL)可以不用不用寫全,就能自動識別出來。
這兩種方法,都得設(shè)置一下setMovementMethod,才會跳轉(zhuǎn)。
另外setAutoLinkMask不僅 識別超鏈接,包括電話號碼之類的。
代碼如下:
第一種
復(fù)制代碼 代碼如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = "有問題:\n";
html+="<a ;//注意這里必須加上協(xié)議號,即http://。
//否則,系統(tǒng)會以為該鏈接是activity,而實(shí)際這個activity不存在,程序就崩潰。
CharSequence charSequence = Html.fromHtml(html);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
第二種
復(fù)制代碼 代碼如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = "有問題:\n";
html+="www.baidu.com";//這里即使不加協(xié)議好HTTP;也能自動被系統(tǒng)識別出來。
textView.setText(html);
textView.setAutoLinkMask(Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
總結(jié)一下就是,以html顯示超鏈接,必須寫全url。以setAutoLinkMask(Linkify.ALL)可以不用不用寫全,就能自動識別出來。
這兩種方法,都得設(shè)置一下setMovementMethod,才會跳轉(zhuǎn)。
另外setAutoLinkMask不僅 識別超鏈接,包括電話號碼之類的。
相關(guān)文章
Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動的漸變效果
這篇文章主要給大家介紹了Android中FloatingActionButton(簡稱FAB)是如何實(shí)現(xiàn)上下滑動的漸變效果,文中給出了詳細(xì)的示例代碼,相信對大家具有一定的參考價值,有需要的朋友們可以一起看看吧。2017-02-02
android獲取照片的快照 思路及實(shí)現(xiàn)方法
android獲取照片的快照 思路及實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-06-06
5分鐘學(xué)會Android設(shè)計模式之策略模式Strategy Pattern教程
這篇文章主要為大家介紹了5分鐘學(xué)會Android設(shè)計模式之策略模式Strategy Pattern教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
使用Android studio查看Kotlin的字節(jié)碼教程
這篇文章主要介紹了使用Android studio查看Kotlin的字節(jié)碼教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android獲取當(dāng)前應(yīng)用分配的最大內(nèi)存和目前使用內(nèi)存的方法
今天小編就為大家分享一篇關(guān)于Android獲取當(dāng)前應(yīng)用分配的最大內(nèi)存和目前使用內(nèi)存的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
Andriod 讀取網(wǎng)絡(luò)圖片實(shí)例代碼解析
Android手機(jī)上,我們經(jīng)常用imageview顯示圖片,通過本文學(xué)習(xí)獲取網(wǎng)絡(luò)圖片并顯示在imageview中,對android讀取網(wǎng)絡(luò)圖片相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-02-02

