Android Development Implementation TextView displays Rich text _android

Source: Internet
Author: User

This article is an example of how the Android development implementation TextView displays Rich text. Share to everyone for your reference, specific as follows:

As shown in the figure, the HTML element control, click Connect to realize the Internet, send email, dial

Implementation of the source code is as follows:

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);
    Add a section of HTML logo 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 ' > Baidu </a></big><br>";
    Charsequence charsequence = html.fromhtml (Html);
    Textview1.settext (charsequence); Textview1.setMovementmethod (Linkmovementmethod.getinstance ())//Click to produce a hyperlink String text = "My url:http://www.sina.com\n";
    Text = "My email:abcd@126.com\n";
    Text + "My Phone: + 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 > How to display HTML element controls </string>
  <color name= "green" > #00FF00 </color>
  <string name= " Link_text "><a href=" tel:13693207964 "> Call </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>

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.