转自:http://www.jb51.net/article/38160.htm
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址:
这两种方法,都得设置一下setMovementMethod,才会跳转。
另外setAutoLinkMask不仅 识别超链接,包括电话号码之类的。
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址:
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 href='http://www.baidu.com'>百度一下</a>";//注意这里必须加上协议号,即http://。
//否则,系统会以为该链接是activity,而实际这个activity不存在,程序就崩溃。
CharSequence charSequence = Html.fromHtml(html);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
一种是设置autolink,让系统自动识别超链接:
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 href='http://www.baidu.com'>百度一下</a>";//注意这里必须加上协议号,即http://。
//否则,系统会以为该链接是activity,而实际这个activity不存在,程序就崩溃。
CharSequence charSequence = Html.fromHtml(html);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
} 总结一下就是,以html显示超链接,必须写全url。以setAutoLinkMask(Linkify.ALL)可以不用不用写全,就能自动识别出来。
这两种方法,都得设置一下setMovementMethod,才会跳转。
另外setAutoLinkMask不仅 识别超链接,包括电话号码之类的。
本文详细介绍了在Android应用的TextView中添加超链接的两种方法:使用HTML样式化网址和设置autoLink属性。同时强调了必须完整填写URL,以及设置setMovementMethod才能实现跳转功能。
&spm=1001.2101.3001.5002&articleId=38490843&d=1&t=3&u=1101f09369454756afb976fb596a8fad)
531

被折叠的 条评论
为什么被折叠?



