Android automatically matches email addresses, URLs, and phone numbers.

Source: Internet
Author: User

Looking at the android API, you have no intention of seeing linkify. According to API instructions, linkify can convert words that match the rules into clickable connections.

I think this is a little useful. Let's try it.

1. Create a project

2. Modify the Style File

Add an edittext and a textview to view the effect.

CodeAs follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: weightsum = "1"> <edittext Android: layout_height = "wrap_content" Android: layout_width = "fill_parent" Android: id = "@ + ID/inputtext"> <requestfocus> </edittext> <textview Android: layout_height = "wrap_content" Droid: textappearance = "? Android: ATTR/textappearancelarge "Android: layout_width =" fill_parent "Android: layout_weight =" 0.08 "Android: Id =" @ + ID/viewtext "Android: TEXT = "Enter the content"> </textview> </linearlayout>

3. Modify androidlearnactivity. Java

Let's take a look at the key parts.

In this example, we use the first one. Let's take a look at what linkify supports by default.

Map_addresses I do not understand...

The complete code is as follows:

 Package CN. ghy. sdx;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. Text. util. linkify;
Import Android. View. keyevent;
Import Android. View. view;
Import Android. View. View. onkeylistener;
Import Android. widget. edittext;
Import Android. widget. textview;

Public Class Androidlearnactivity Extends Activity {
Edittext;
Textview;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Edittext = (edittext)This . Findviewbyid (R. Id. inputtext );
Textview = (textview) This . Findviewbyid (R. Id. viewtext );
Edittext. setonkeylistener ( New Onkeylistener (){

@ Override
Public Boolean Onkey (view V, Int Keycode, keyevent event ){
Textview. settext (edittext. gettext ());
Linkify. addlinks (textview, linkify. email_addresses | linkify. phone_numbers | linkify. web_urls );
Return False ;
}
});
}
}

 

Effect:

URL:

Tel:

Email:

Advanced learning:

The Several built-in functions of linkify can meet the needs, but sometimes they have more flexible needs. For example, you need to enter an ID card number.

I am not very clear about regular expressions. It is easy to use.

 
Textview. settext (edittext. gettext ());
Pattern pattern = pattern. Compile ("\ D {15} | \ D {18 }");
String scheme = "http: // chaxun? Sfz = ";//This is free to write.
Linkify. addlinks (textview, pattern, scheme );

Effect:

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.