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.