Linkify for Android Development
/*
* Popupwindow for Android Development
*
* Created on: 2011-8-11
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
Linkify is a helper class that automatically creates a hyperlink by matching the RegEx pattern in the textview class (or the textview derived class.
Text that matches a specific RegEx pattern will be converted into a clickable hyperlink, these Hyperlinks can implicitly use matched strings as the target URI to trigger startactivity (new intent (intent. action_view, Uri )). You can specify any string mode to be converted to a connection.
Static linkify. the addlinks method can establish links by accepting views. The linkify class provides and supports one or more default Content-Type bitmasks (bitmask): web_urls, email_addresses, phone_numbers, and all.
The following code snippet shows how to use textview to display web and e-mail addresses as hyperlinks. When you click these hyperlinks, they will open the browser or email server respectively.
You can use the Android: autolink attribute to linkify the view within a layout resource. It supports one or more of the following values (separated by |): None, web, email, phone, or all.
You can make the following settings in the layout file and Java file:
<Textview Android: Id = "@ + ID/mytextview1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: autolink = "Web | email | phone">
</Textview>
By setting
Mytextview1.settext (http://www.baidu.com ");
Mytextview1.settext (13800138000 ");
Mytextview1.settext (hello@163.com ");
You can also set a string in the string file. Similarly, it must be separated by the | character.
Next, let's explain the custom linkify: after searching, almost all the articles on the Internet are copied from the android advanced programming book. I hope you can learn more, it's boring to copy it.
A custom linkify can detect a specific string and convert it to a clickable string to get the desired string from a "Source. In the manual, write addlinks containing three parameters as follows:
Applies a RegEx to the text of a textview turning the matches into links. if links are found then urlspans are applied to the link text match areas, and the Movement method for the text is changed to linkmovementmethod.
Use the following code to resolve the problem:
IntFlags = pattern.Case_insensitive;
Pattern P = pattern.Compile("\ Bnews [0-9] * \ B", flags );
Linkify.Addlinks(Mytextview, P, "http://www.baidu.com /");
These lines of code indicate: [0-9] * Indicates a wildcard. Either 0 or 9 does not exist, \ B Indicates filtering out the previous \ B. Of course, you can also write it as "news [0-9, after being added to http://www.baidu.com/, for example, after mytextview1.settext ("news1 | news2 | News") is set, the execution result will be three words that can be clicked, and then connected to the response ://.
Of course, the above method does not apply to creating a text hyperlink. For text hyperlinks, you can use the following code:
Spannablestring sp =NewSpannablestring ("Click Baidu ");
// Set the hyperlink
Sp. setspan (NewUrlspan ("http://www.baidu.com"), 2, 4,
Spanned.Span_exclusive_exclusive);
Mytextview. settext (SP );
Mytextview. setmovementmethod (linkmovementmethod.Getinstance());
Everything else is okay. Let's see that 2 and 4 indicate that the string ends from the third character to the fourth character. The last sentence indicates that the text can be clicked.
I think there is no particular research value for linkify matchfilter and transform filter. It is not described here.