Andoid automatically determines whether the input is a phone number, URL, or email-The linkify application!

Source: Internet
Author: User

This section describes howEdittextEnter the phone number or URL orEmailWhen, letAndroidAutomatic judgment: when we enter the phone number, we click the input content to call the call program. When we enter the Web site, click to open the browser program.LinkifyThis problem is solved in four steps.Demo.

 

Step 1: CreateAndroidProject, namedLinkifydemo.

 

Step 2:OpenMain. xmlFile to the following content:

 

<? 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"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Enter your phone number, email address, or URL :"
/>
<Edittext
Android: Id = "@ + ID/ET1"
Android: layout_width = "340px"
Android: layout_height = "wrap_content"
/>

<Textview
Android: Id = "@ + ID/TV1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

</Linearlayout>

Step 3:In the main applicationLinkifydemo.JavaThe code is modified as follows:

 

Package com. Android. test;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. Text. util. linkify;
Import Android. View. keyevent;

Import Android. View. view;
Import Android. widget. edittext;
Import Android. widget. textview;

Public class linkifydemo extends activity {

Private edittext et;
Private textview TV;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

// Obtain resources
ET = (edittext) findviewbyid (R. Id. ET1 );
TV = (textview) findviewbyid (R. Id. TV1 );

// Add Event Response
Et. setonkeylistener (New edittext. onkeylistener ()
{
@ Override
Public Boolean onkey (view V, int keycode, keyevent event ){
TV. settext (ET. gettext ());
// Determine the input type and connect it to the System
Linkify. addlinks (TV, linkify. web_urls |
Linkify. email_addresses | linkify. phone_numbers );

Return false;
}
});
}
}

 

 Step 4:The following results will be displayed during running:

 

 

Take the phone number as an example, that is, the picture in the upper right corner. When we click this number, the system will automatically call the application called, for example:

 

 

Extended learning:

 

Of course, we still have a simpler method.Main. xmlLiIDIsTVOfTextviewThis statement is:

 

<Textview
Android: Id = "@ + ID/TV1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

Android: autolink = "Web | phone | email"
/>

The same effect can be achieved. Well, today is the end. Thank you!

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.