New ui-three ways to set a hyperlink jump for TextView

Source: Internet
Author: User

New ui-three ways to set a hyperlink jump for TextView

--Reprint Please indicate source: Coder-pig, welcome reprint, do not use for commercial use!


Piglet Android Development Exchange Group has been established, welcome everyone to join, both novice, rookie, Big God can, piglet a person's

Strength is limited after all, the writing out of things will certainly have a lot of flaws, welcome to point out, brainstorm, let the pig's blog

More detailed, help more people, O (∩_∩) o Thank you!

Piglet Android Development Exchange Group: Piglet Android Development Exchange Group Group number: 421858269

new Android UI Instance Daquan directory: http://blog.csdn.net/coder_pig/article/details/42145907




Introduction to this section:

This section is mainly about how to set up hyperlinks for TextView, jump to browser, SMS, dial-up interface, etc.

Methods three kinds were used:

① uses spannablestring to achieve

② uses the Autolink property to implement the

③ uses HTML <a> tags to implement

So start the content of this section!


The text of this section:

1. Set a hyperlink via spannablestring:

Through spannablestring, the core approach is to:

Setspan (Object what, int start, int end, int flags)

Mainly see start and end, one is the starting subscript of the character, counting from 0, end is the end,

The character that contains the starting position, not the end position Oh! In addition, Chinese and English are counted as one character!


The sample code is as follows:

SpannableString mspannablestring = new Spannablestring ("Open Baidu, make calls, send SMS, send mail, send MMS, open map");//Set Hyperlink (Need to add Setmovementmethod method additional 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"), 14,spanned.span_exclusive_exclusive); SMS Using SMS: Or Smsto:mSpannableString.setSpan ("mailto:[email protected"), Urlspan, Spanned.span_ exclusive_exclusive); Mail Mspannablestring.setspan (New Urlspan ("mms:13756565654"), 24,spanned.span_exclusive_exclusive); MMS using MMS: or Mmsto:mSpannableString.setSpan (New Urlspan ("geo:38.899533,-77.036476"), 25,29, spanned.span_exclusive _exclusive); Map Txtshow.setmovementmethod (Linkmovementmethod.getinstance ()); Txtshow.settext (mspannablestring); 

Run:


PS: There is no use of sending mail and opening maps, because Android phones in the celestial kingdom are usually

Castrated, some of Google's services have been removed ~




2. Set by the Autolink property in XML:

In addition to the above method, we can also set different types of hyperlinks in XML through the Autolink property, using the following:



All is included, automatically identify the protocol header ~

Setautolinkmask (Linkify.all) can be called in Java code;

This time can not write the protocol header, Autolink will automatically recognize, but need to set:

Setmovementmethod (Linkmovementmethod.getinstance ());

Otherwise click will not jump to ~




3. Set by <a> hyperlink in HTML:

Another way is through the HTML <a> hyperlink tag, the code is as follows:

Note that the protocol number, or http://, must be added here. Otherwise, the system will assume that the link is activity, and the actual activity does not exist and the program crashes. String html = "<a href= ' http://www.baidu.com ' > Baidu </a>"; Charsequence charsequence = html.fromhtml (Html); Txtshow.settext (charsequence); Txtshow.setmovementmethod ( Linkmovementmethod.getinstance ());

Run:



PS: about how to get rid of the link below, temporarily haven't found the method, know the friend can tell the next ~ thank you very much!!



Here's how to add a hyperlink to TextView


New ui-three ways to set a hyperlink jump for TextView

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.