New UI-set three methods for hyperlink jump for TextView, ui-textview
New UI-three methods for setting hyperlink redirect for TextView
-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!
The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.
After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.
For more details, help more people, O (∩ _ ∩) O thank you!
Piggy Android Development Exchange Group:Piggy Android Development GroupGroup Number:421858269
New Android UI instance catalog:Http://blog.csdn.net/coder_pig/article/details/42145907
This section introduces:
This section describes how to set hyperlinks for TextView, jump to browsers, text messages, and dial-up interfaces.
Three methods are used:
① Implemented using SpannableString
② Use the autoLink attribute for implementation
③ Use HTML <a> tags to implement
Start the content of this section!
Body of this section:
1. Set the hyperlink through SpannableString:
Implemented through SpannableString, the core method is:
SetSpan (Object what, int start, int end, int flags)
Mainly look at start and end. One is the starting subscript of the character, starting from 0, and end is the end,
It contains the starting position, not the ending position! In addition, both Chinese and English are counted as one character!
The sample code is as follows:
SpannableString mSpannableString = new SpannableString ("Open Baidu, call, send SMS, send email, send MMS, open map"); // set the hyperlink (you need to add the setMovementMethod method to add a response) mSpannableString. setSpan (new URLSpan ("http://www.baidu.com"), 0, 4, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableString. setSpan (new URLSpan ("tel: 13756565654"), 5, 9, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // phone mSpannableString. setSpan (new URLSpan ("sms: 13756565654"), 10, 14, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // sms: Or smsto: mSpannableString. setSpan (new URLSpan ("mailto: 779878443@qq.com"), 15, 19, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // email mSpannableString. setSpan (new URLSpan ("mms: 13756565654"), 20, 24, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mms: Or mmsto: mSpannableString. setSpan (new URLSpan ("geo: 38.899533,-77.036476"), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // map txtShow. setMovementMethod (LinkMovementMethod. getInstance (); txtShow. setText (mSpannableString );
Run:
Ps: emails sent and maps opened here are useless, because tianchaoli Android phones are usually
Some Google services have been removed ~
2. Use the autoLink attribute in XML to set:
In addition to the above method, we can also set different types of hyperlinks in XML using the autoLink attribute. The usage is as follows:
All is included. The protocol header is automatically recognized ~
In Java code, you can call setAutoLinkMask (Linkify. ALL );
In this case, you do not need to write the protocol header. autolink automatically identifies the protocol, but you need to set it:
SetMovementMethod (LinkMovementMethod. getInstance ());
Otherwise, you won't jump to the page when you click it ~
3. Set the parameter through the <a> hyperlink in HTML:
Another method is to use the <a> hyperlink tag of html. The Code is as follows:
// Note that the Protocol number must be added here, that is, http ://. Otherwise, the system will think that the link is an activity, but the actual activity does not exist, and the program will crash. String html = "<a href = 'HTTP: // www.baidu.com '> Baidu </a>"; CharSequence charSequence = Html. fromHtml (html); txtShow. setText (charSequence); txtShow. setMovementMethod (LinkMovementMethod. getInstance ());
Run:
Ps: No methods have been found for how to remove the following link. You can tell me if you know it ~ Thank you !!
Let's talk about how to add hyperlinks to TextView ~