textView 添加超鏈接(兩種實現(xiàn)方式)
更新時間:2013年06月08日 15:29:24 作者:
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設置autolink,讓系統(tǒng)自動識別超鏈接,下面為大家介紹下這兩種方法的實現(xiàn)
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設置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,而實際這個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)可以不用不用寫全,就能自動識別出來。
這兩種方法,都得設置一下setMovementMethod,才會跳轉(zhuǎn)。
另外setAutoLinkMask不僅 識別超鏈接,包括電話號碼之類的。
代碼如下:
第一種
復制代碼 代碼如下:
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,而實際這個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)可以不用不用寫全,就能自動識別出來。
這兩種方法,都得設置一下setMovementMethod,才會跳轉(zhuǎn)。
另外setAutoLinkMask不僅 識別超鏈接,包括電話號碼之類的。
相關(guān)文章
android H5本地緩存加載優(yōu)化的實戰(zhàn)
這篇文章主要介紹了android H5本地緩存加載優(yōu)化的實戰(zhàn),幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-04-04Android中HttpURLConnection與HttpClient的使用與封裝
這篇文章主要介紹了Android中HttpURLConnection與HttpClient的使用以及封裝方法,感興趣的小伙伴們可以參考一下2016-03-03Android開發(fā)數(shù)據(jù)結(jié)構(gòu)算法ArrayList源碼詳解
這篇文章主要為大家介紹了Android開發(fā)數(shù)據(jù)結(jié)構(gòu)算法ArrayList源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10