本文实例讲述了android开发实现textview显示丰富的文本的方法。分享给大家供大家参考,具体如下:
如图,显示html的元素控件,点击连接实现上网,发email,拨号
实现源码如下:
mainactivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.example.textview2; import android.os.bundle; import android.app.activity; import android.text.html; import android.text.method.linkmovementmethod; import android.view.menu; import android.widget.textview; public class mainactivity extends activity { private textview textview1, textview2; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview1 = (textview) this .findviewbyid(r.id.textview1); textview2 = (textview) this .findviewbyid(r.id.textview2); // 添加一段html的标志 string html = "<font color='red'></font><br><br><br>" ; html += "<font color='#0000ff'><big><i></i></big></font><p>" ; html += "<big><a href='http://www.baidu.com'>百度</a></big><br>" ; charsequence charsequence = html.fromhtml(html); textview1.settext(charsequence); textview1.setmovementmethod(linkmovementmethod.getinstance()); // 点击的时候产生超链接 string text = "我的url:http://www.sina.com\n" ; text += "我的email:abcd@126.com\n" ; text += "我的电话:+ 86 010-89487389" ; textview2.settext(text); textview2.setmovementmethod(linkmovementmethod.getinstance()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true ; } } |
strings.xml
1
2
3
4
5
6
7
8
|
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <string name= "action_settings" >settings</string> <string name= "hello_world" >hello world!</string> <string name= "app_name" >如何显示html的元素控件</string> <color name= "green" >#00ff00</color> <string name= "link_text" ><a href= "tel:13693207964" >打电话</a></string> </resources> |
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".mainactivity" > <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/textview1" android:padding= "20sp" /> <textview android:id= "@+id/textview2" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:autolink= "all" android:padding= "20sp" android:text= "@string/link_text" android:textsize= "20sp" /> </relativelayout> |
希望本文所述对大家android程序设计有所帮助。