Android---10---TextView添加链接的两种方式
2015-04-14 19:46
465 查看
在TextView中显示带Html文本
显示丰富的内容:URL、字体大小、颜色等
在TextView中预定了一些类似于HTML的标签,通过标签可以使TextView控件显示不同颜色、大小、字体的文字
<font> 设置颜色和字体
<big> 设置大号
<small> 设置小号
<i>、<b> 斜体、粗体
<a> 链接地址
<img> 图片
使用这些标签可以用Html.fromHtml方法将这些标签的字符串转换成Charsequence对象,然后在TextView中进行设置。
Charsequence为接口类型,String类型就是它的子类,它包含了字符串的所有类。
Charsequence与String的区别:Charsequence与String都能用于定义字符串,但Charsequence的值是可读可写序列,String的值是只读序列。
两种显示的方法,一种是使用带html标签的,一种是使用autolink的
第一种:使用带html标签的文本:
activity_main.xml:
MainActivity.java:
第二种:使用autolink:
activity_main.xml:
MainActivity.java:
显示丰富的内容:URL、字体大小、颜色等
在TextView中预定了一些类似于HTML的标签,通过标签可以使TextView控件显示不同颜色、大小、字体的文字
<font> 设置颜色和字体
<big> 设置大号
<small> 设置小号
<i>、<b> 斜体、粗体
<a> 链接地址
<img> 图片
使用这些标签可以用Html.fromHtml方法将这些标签的字符串转换成Charsequence对象,然后在TextView中进行设置。
Charsequence为接口类型,String类型就是它的子类,它包含了字符串的所有类。
Charsequence与String的区别:Charsequence与String都能用于定义字符串,但Charsequence的值是可读可写序列,String的值是只读序列。
两种显示的方法,一种是使用带html标签的,一种是使用autolink的
第一种:使用带html标签的文本:
activity_main.xml:
<TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
MainActivity.java:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textview1); String html = "<font color='red'>I love Android</font><br>"; html += "<a href='http://www.baidu.com' ><big>百度</big></a>"; CharSequence charSequence = Html.fromHtml(html); textView.setText(charSequence); //点击事件的响应,如果没有这个的话,点击链接没有反应 textView.setMovementMethod(LinkMovementMethod.getInstance()); } }
第二种:使用autolink:
activity_main.xml:
<TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="all"/>
MainActivity.java:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView2 = (TextView) findViewById(R.id.textview2); String text = "网址:http://www.baidu.com"+"\n"; text += "邮箱:123123132@163.com"+"\n"; text += "电话:45612132" +"\n"; textView2.setText(text); } }
相关文章推荐
- Android(java)学习笔记147:textView 添加超链接(两种实现方式,,区别于WebView)
- Android textView 添加超链接(两种实现方式)
- Android 编程下 TextView 添加链接的一种方式
- android textView 添加超链接(两种实现方式)
- Android 编程下 TextView 添加链接的一种方式
- 给TextView中的部分文本添加链接的两种方式
- Android TextView 添加下划线的几种方式
- android里TextView加下划线的几种方式 以及 为TextView添加边框 4000
- android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- Android TextView 添加连接方式
- Android中动态添加View的两种方式
- Android 实现TextView中 文字链接的方式
- android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- android textView WebView 两种方式 显示html 格式文本 带图片