1.显示“http://www.baidu.com.cn”,点击它跳转到百度网页
step1.在layout中为textview添加 android:autoLink="all"或android:autoLink="web"
step2.在java代码中,添加如下语句:
txt1=(TextView)this.findViewById(R.id.txt1);
txt1.setText("http://www.baidu.com.cn");
txt1.setMovementMethod(LinkMovementMethod.getInstance());
效果如下图所示,点击“http://baidu.com.cn”即可跳转到百度页面
2.显示“baidu”,点击跳转到百度网页
step1:取消xml中的 android:autoLink="all"
step2:java中添加代码如下
txt1=(TextView)this.findViewById(R.id.txt1);
txt1.setText(
Html.fromHtml(
"<a href=\"http://www.baidu.com\">baidu</a> "));
txt1.setMovementMethod(LinkMovementMethod.getInstance());
效果图如下,点击“baidu”即可跳转到百度页面,