Android ApiDemos example (92): Text-& gt; Linkify

Source: Internet
Author: User

Linkify provides a helper class for TextView and its subclass to automatically convert text strings into hyperlinks. Of course, text strings must comply with predefined or custom regular expressions.

After a text string that complies with the regular expression (RegEx Pattern) is converted to a hyperlink, When you click the hyperlink, startActivity (new Intent (Intent. ACTION_VIEW, uri) uri is a string that conforms to the definition.

Linkify allows you to use any regular expression to match text. For convenience, Android comes with several regular expressions to match phone numbers, emails, and Http links.

Static Method of Linkify addLinks

Public static final boolean addLinks (TextView text, int mask)

Text is a TextView object.
Mask Can Be EMAIL_ADDRESSES, MAP_ADDRESSES, PHONE_NUMBERS, WEB_URLS, and so on, indicating the regular expression types that can be matched in the system. ALL indicates ALL types
Use the code to add a hyperlink to TextView for Automatic Detection:

[Java]
TextView textView = (TextView) findViewById (R. id. myTextView );
Linkify. addlinks (textView, Linkify. WEB_URLS | Linkify. EMAIL_ADDRESSES );

TextView textView = (TextView) findViewById (R. id. myTextView );
Linkify. addlinks (textView, Linkify. WEB_URLS | Linkify. EMAIL_ADDRESSES); you can also use the android: autoLink attribute of TextView in Layout to set the hyperlink type, for example, TextView text1 in this example.

<TextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/text1 ″
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: autoLink = "all"
Android: text = "@ string/link_text_auto"
/>

Linkify also allows you to customize the pattern and matching algorithms for RegEx, which is not described in this example and For details, see http://developer.android.com/reference/android/text/util/Linkify.html

Related classes include Pattern and MatchFilter.

In this example, the String resource displayed in text2 is

<B> text2: </B> This is some other
Text, with a <a href = "http://www.google.com"> link </a> specified
Via an & lt; a & gt; tag. Use a \ "tel: \" URL
To <a href = "tel: 4155551212"> dial a phone number </a>

This is an HTML text string, where a is an HTML link. It is also displayed as a hyperlink when Android TextView is displayed. However, by default, no action is taken when users click this link. You need to use setMovementMethod to add the LinkMovementMethod when clicking a hyperlink.

[Java]
TextView t2 = (TextView) findViewById (R. id. text2 );
T2.setMovementMethod (LinkMovementMethod. getInstance ());

TextView t2 = (TextView) findViewById (R. id. text2 );
T2.setMovementMethod (LinkMovementMethod. getInstance (); text3 is similar to text2. It only sets HTML text through code instead of displaying text from the resource file.

[Java]
TextView t3 = (TextView) findViewById (R. id. text3 );
T3.setText (
Html. fromHtml (
"<B> text3: </B> Text with a" +
"<A href = \" http://www.google.com \ "> link </a>" +
"Created in the Java source code using HTML ."));
T3.setMovementMethod (LinkMovementMethod. getInstance ());

TextView t3 = (TextView) findViewById (R. id. text3 );
T3.setText (
Html. fromHtml (
"<B> text3: </B> Text with a" +
"<A href = \" http://www.google.com \ "> link </a>" +
"Created in the Java source code using HTML ."));
T3.setMovementMethod (LinkMovementMethod. getInstance (); text4 uses the SpannableString class

SpannableString defines a read-only string, but it can dynamically add or delete a part of the string in the displayed format. Supported formats are defined in the package android. text. style, including alignment, scaling, and other formats.

[Java]
SpannableString ss = new SpannableString (
"Text4: Click here to dial the phone .");

Ss. setSpan (new StyleSpan (Typeface. BOLD), 0, 6,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
Ss.setspan (new URLSpan ("tel: 4155551212"), 13, 17,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );

TextView t4 = (TextView) findViewById (R. id. text4 );
T4.setText (ss );
T4.setMovementMethod (LinkMovementMethod. getInstance ());

SpannableString ss = new SpannableString (
"Text4: Click here to dial the phone .");
 
Ss. setSpan (new StyleSpan (Typeface. BOLD), 0, 6,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
Ss.setspan (new URLSpan ("tel: 4155551212"), 13, 17,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
 
TextView t4 = (TextView) findViewById (R. id. text4 );
T4.setText (ss); www.2cto.com
T4.setMovementMethod (LinkMovementMethod. getInstance (); text4 displays the text text4: Click here to dial the phone. ", and then uses setSpan to change the format of text4 to bold. Here is a hyperlink, And the link address is a phone number.

Public void setSpan (Object what, int start, int end, int flags)

What: format type
Start: start position
End: the end position.
Flags: The identifier.

 
Author: mapdigit
 


 


 

 


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.