Android automatically determines the phone number, URL, and EMAIL-based Linkify usage.

Source: Internet
Author: User

When weEditTextEnter the phone number or URL orEmailWhen we enter a phone number, we click the input content to call the call program. When we enter a Web site, click to open the browser program.LinkifySolved this problem.

Steps:

1. layout UI

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/TV"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>

<EditText
Android: id = "@ + id/et"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>

<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</LinearLayout>

2. Implement in MainActivity
Copy codeThe Code is as follows: public class MainActivity extends Activity {

Private TextView TV;
Private EditText et;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

TV = (TextView) findViewById (R. id. tv1 );
Et = (EditText) findViewById (R. id. et );
Et. setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
TV. setText (et. getText ());
// Determine whether the entered URL, EMAIL, or PHONENUMBER, and automatically connect to the System
Linkify. addLinks (TV, Linkify. WEB_URLS | Linkify. EMAIL_ADDRESSES | Linkify. PHONE_NUMBERS | );
Return false;
}
});
}
}

OK! Simple Method: declare in TextView as follows!

<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

Android: autoLink = "web | phone | email"
/>

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.