TextView two display link methods, textview two link

Source: Internet
Author: User

TextView two display link methods, textview two link
TextView two display link Methods

I. Introduction

It is also a TextView display text control method.

It also displays rich texts.

 

Ii. Method

TextView two display link Methods

1) Use html-like labels in TextView
* 1. Set the html Tag text.

String text1 = "<font color = 'red'> <I> hello, stranger </I> </font> <br/> ";
Text1 + = "<a href = 'HTTP: // www.baidu.com '> Baidu </a> <br/> ";

* 2. Declare Html. fromHtml for the previous text so that TextView can be parsed as an html Tag.

TV _one.setText (Html. fromHtml (text1 ));
* 3. Set link click events

TV _one.setMovementMethod (LinkMovementMethod. getInstance ());


2) use the android: autoLink attribute
* 1. Add plain text

String text2 = "My Website: http://www.baidu.com \ n ";
Text2 + = "My Phone: 18883306749 ";
TV _two.setText (text2 );

* 2. Set the android: autoLink attribute in the textView of layout.

Android: autoLink = "all"

 

Iii. code example

Click the Baidu link above and the Baidu link below. Appears

Click the phone number. Appears

 

Code:

Fry. Activity01

1 package fry; 2 3 import com. example. textViewDemo1.R; 4 5 import android. app. activity; 6 import android. OS. bundle; 7 import android. text. html; 8 import android. text. method. linkMovementMethod; 9 import android. widget. textView; 10 11 public class Activity01 extends Activity {12 private TextView TV _one; 13 private TextView TV _two; 14 @ Override15 protected void onCreate (Bundle savedInstanceState) {16 // TODO Auto-generated method stub17 super. onCreate (savedInstanceState); 18 setContentView (R. layout. activity01); 19 20 TV _one = (TextView) findViewById (R. id. TV _one); 21 TV _two = (TextView) findViewById (R. id. TV _two); 22 23/* 24 * TextView two display link Methods 25*1) use the class html Tag 26*1 in TextView, set the html Tag text 27*2, and declare Html for the previous text. fromHtml makes it easy to parse TextView into html Tag 28*3. Set link Click Event 29*30*2) through android: autoLink property 31*1. Add plain text 32*2. Set android in layout's textView: autoLink property 33*34 */35 36 // display effect is achieved through html-like labels in TextView 37String text1 = "<font color = 'red'> <I> hello, stranger </I> </font> <br/> "; 38 text1 + = "<a href = 'HTTP: // www.baidu.com '> Baidu </a> <br/> ";39 40TV _one.setText (Html. fromHtml (text1 ));41 // set the mouse movement event to generate a link display. If this sentence is not found, Baidu 42 TV _one.setMovementMethod (LinkMovementMethod. getInstance (); 43 44 // android: autoLink = "all" is set in TV _two, that is, all links are automatically displayed.45 String text2 = "My Website: http://www.baidu.com \ n"; 46 text2 + = "My Phone: 18883306749"; 47 TV _two.setText (text2 );48 // because I have set the android: autoLink attribute, you can enter the Baidu page without the following sentence. Enter the phone page 49 // TV _two.setMovementMethod (LinkMovementMethod. getInstance (); 50 51 52 53} 54}

/TextViewDemo1/res/layout/activity01.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:id="@+id/tv_one" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content" />11     12     <TextView13         android:id="@+id/tv_two"14         android:layout_width="match_parent"15         android:layout_height="wrap_content" 16         android:autoLink="all"17         />18 19 </LinearLayout>

 

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.