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"
/>