Android strings are automatically associated with corresponding programs, mainly including email, phone, and Web

Source: Internet
Author: User

Do you really want the text in your textview to be displayed as a link when you encounter a phone number or webpage address? Then, when we click it, the corresponding phone or browser application will be displayed?

If your answer is yes, let's take a look at my applications here. The Code is as follows:

Code of the layout file linkify. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> <! -- If you want to set it in the XML layout file <br/> Android: autolink = "all", this is where automatic association is set. <br/> the main values are: none/All/web/email/phone/Map <br/> --> <br/> <textview <br/> Android: id = "@ + ID/textview0" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "15801189818" <br/> </textview> </P> <p> </linearlayout> <br/>

Activity Code:

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. text. util. linkify; <br/> Import android. widget. textview; </P> <p> public class linkifyactivity extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. linkify); <br/> textview textv Iew = (textview) findviewbyid (R. id. textview0); </P> <p>/** <br/> * The addlinks method of the linkify class, you can add one or more matches for US (multiple matches are separated by |). <br/> * when our content and rules match, the system automatically calls the corresponding applications in the system for processing. <Br/> */<br/> linkify. addlinks (textview, linkify. phone_numbers | linkify. web_urls); </P> <p >}< br/>}

In essence, we use the addlinks method of the linkify class.

The addlinks method obtains the content of the textview that we pass in, finds the matching characters in it, and presents it to the user through the link. When the user clicks the link, requests will be sent to the application to be started through some intent of the system.

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.