Android implements text links in textview

Source: Internet
Author: User

Android implements text links in textview

There are many ways to implement text links in textview in Android.
In summary, there are four types:
1. when the URL, email, and phone number appear in the text, you can set the Android: autolink attribute of textview to the corresponding value, if all types are available, Android: autolink = "all" is used ". Of course, you can alsoCodeTextview01.setautolinkmask (linkify. All );
2. Write the text to be processed to a resource file, such as string. XML, and then reference it in the Java code (Directly Writing it in the Code is not feasible, and the text will be displayed and processed directly)
3. Use the fromhtml () method of the HTML class to format the text to be placed in the textview.
4. Use spannable or its class, such as spannablestring, to format some strings.

Remember to add:
Textview. setmovementmethod (linkmovementmethod. getinstance ());

Example:
Setcontentview (R. layout. test );

String source = "<B> <font color = # ff0000> HTML view using textview"
+ "</Font> </B> <br> <a href = 'HTTP: // www.androidpeople.com '> androidpeople.com </a>"
+ "<Br> <a href = 'HTTP: // www.android.com '> android.com </a> ";

Textview = (textview) findviewbyid (R. Id. textview01 );
Textview. settext (html. fromhtml (source ));
// Used to enable links in textview.
Textview. setmovementmethod (linkmovementmethod. getinstance ());

Or:
Textview textview01 = (textview) findviewbyid (R. Id. textview01 );
Textview01.setautolinkmask (linkify. All );
String autolinktext = "http://student.csdn.net /? 232885 my csdn blog ";
Textview01.settext (autolinktext );

Partial connections:
Spannablestring Ss = new spannablestring ("Call: 4155551212 .");
SS. setspan (New stylespan (typeface. Bold), 0, 5, spanned. span_exclusive_exclusive );
SS. setspan (New urlspan ("Tel: 4155551212"), 6, 16, spanned. span_exclusive_exclusive );
Textview04.settext (SS );
Textview04.setmovementmethod (linkmovementmethod. getinstance ());
Only Part 1 is specified as a connection.

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.