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!