Android TextView Example of how to add a hyperlink _android

Source: Internet
Author: User

The example in this article describes the Android TextView method for adding hyperlinks. Share to everyone for your reference, specific as follows:

public class Link extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreat
    E (savedinstancestate);
    Setcontentview (R.layout.link);
    Text1 shows the Android:autolink property, which//automatically linkifies things like URLs and phone numbers Found in the text.
    No Java code is needed to make this//work. Text2 has links specified by putting <a> tags in the string//resource. By default these links would appear but not//respond to user input.
    To make them active and you are need to//call Setmovementmethod () on the TextView object.
    TextView t2 = (TextView) Findviewbyid (R.ID.TEXT2);
    T2.setmovementmethod (Linkmovementmethod.getinstance ()); Text3 shows creating text with links to HTML in the Java//code, rather than from a string resource. This is the for a//fixed string, using a (localizable) resource as shown above/are usually a better way to go; This example is inteNded to//illustrate I/might display text that came from A//dynamic source (eg, the network).
    TextView t3 = (TextView) Findviewbyid (R.ID.TEXT3); T3.settext (html.fromhtml ("<b>text3:</b> Text with a" + "<a href=\" Http://www.googl
    E.com\ ">link</a>" + "created in the Java source code using HTML");
    T3.setmovementmethod (Linkmovementmethod.getinstance ()); TEXT4 illustrates constructing a styled string containing a//link without using HTML at all.
    Again, for a fixed string//You should probably is using a string resource, not A//hardcoded value.
    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"), N, spanned.span_exclusive_exclusive);
 TextView T4 = (TextView) Findviewbyid (R.ID.TEXT4);   T4.settext (ss);
  T4.setmovementmethod (Linkmovementmethod.getinstance ()); }
}

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Layout Layout Skills Summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.