当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发实现TextView显示丰富的文本

Android开发实现TextView显示丰富的文本

2019年07月24日  | 移动技术网IT编程  | 我要评论

苏州家教网,宠盲妻,中国银行网银首次登陆

本文实例讲述了android开发实现textview显示丰富的文本的方法。分享给大家供大家参考,具体如下:

如图,显示html的元素控件,点击连接实现上网,发email,拨号

实现源码如下:

mainactivity.java

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

<?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

<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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网