Android implementation of TextView text links in 4 ways introduction and code _android

Source: Internet
Author: User
There are a number of ways to implement TextView text links in Android.

In summary, there are about 4 kinds.
1. When the text appears in the URL, E-mail, telephone number, etc., you can set the TextView Android:autolink property to the corresponding value, if all types come out is android:autolink= "all". Of course, can also be done in Java code, textview01.setautolinkmask (Linkify.all);
2. The text to be processed is written to a resource file, such as String.xml, and then referenced in the Java code (written directly in the code is not feasible, will directly display the text to be processed)
3. Format the text to be placed in the TextView with the fromhtml () method of the HTML class
4. Use spannable or implement it's class, such as spannablestring to format, part of the string.

finally remember to add:
Copy Code code as follows:

Textview.setmovementmethod (Linkmovementmethod.getinstance ());

Example:
Copy Code code as follows:

[Code]
Setcontentview (r.layout.test);
String Source = "<b><font color= #ff0000 > Html View using TextView"
+ "</font></b><br><br><a href= ' http://www.AndroidPeople.com ' >AndroidPeople.com</a>"
+ "<br><br><a href= ' http://www.Android.com ' >Android.com</a>";
TextView TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW01);
Textview.settext (source) (html.fromhtml);
Used to enable links in TextView.
Textview.setmovementmethod (Linkmovementmethod.getinstance ());

Or:
Copy Code code as follows:

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:
Copy Code code as follows:

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, spanned.span_exclusive_exclusive);
Textview04.settext (ss);
Textview04.setmovementmethod (Linkmovementmethod.getinstance ());

There are only 4,155,551,212 parts to be connected.
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.