textView 添加超链接
来源:互联网 发布:文明5 for mac 汉化 编辑:程序博客网 时间:2024/05/20 08:00
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置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);
}
第二种:
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";//这里即使不加协议好HTTP;也能自动被系统识别出来。
textView.setText(html);
textView.setAutoLinkMask(Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
总结一下就是,以html显示超链接,必须写全url。以setAutoLinkMask(Linkify.ALL)可以不用不用写全,就能自动识别出来。
这两种方法,都得设置一下setMovementMethod,才会跳转。
另外setAutoLinkMask不仅 识别超链接,包括电话号码之类的。
- textView 添加超链接
- textview 添加超链接
- textview添加超链接
- textview添加超链接
- Android 中TextView 添加超链接
- 在TextView中添加超链接
- Android在文本中添加超链接 (TextView)
- Android在文本中添加超链接 (TextView)
- textView添加超链接(两种方式)
- android TextView添加超链接,改变颜色等
- android--textView添加html超链接响应
- Android给textview添加超链接事件
- TextView超链接
- Textview超链接
- Android开发:TextView添加超链接的简便方法
- 给TextView添加超链接的四种方式
- 关于TextView中怎么添加超链接的问题
- TextView中添加超链接的四种方式
- 浅谈ClassLoader
- Write an Efficient C Program to Reverse Bits of a Number
- Android ViewPager使用详解
- Extjs Tab closeTab 显示中文
- Loadrunner的使用
- textView 添加超链接
- c++ 如何获取系统时间??
- 【文章教程】win 2003系统设置有哪些诀窍?
- 回调接口
- mac杂项
- 经典语录
- notification通过intent传递参数到activity(多个消息参数一样)
- 常见的文字编码格式
- 《算法导论》学习笔记