当前位置: 移动技术网 > IT编程>移动开发>Android > Android 自动判断是电话,网址,EMAIL方法之Linkify的使用

Android 自动判断是电话,网址,EMAIL方法之Linkify的使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

黄南,本月上映电影,进化心理学

当我们在一个edittext输入电话或者网址还是email的时候,让android自动判断,当我们输入的是电话,我们点击输入内容将调用打电话程序,当我们输入是网址点击将打开浏览器程序.而linkify很好的解决了这个问题

步骤:

1、布局ui

复制代码 代码如下:

<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、在mainactivity中实现
复制代码 代码如下:

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());
    // 判断输入的是url还是email还是phonenumber,并自动与系统连接
    linkify.addlinks(tv, linkify.web_urls | linkify.email_addresses | linkify.phone_numbers |);
    return false;
   }
  });
 }
}


ok!简便方法:在textview中如下申明!

<textview
 android:id="@+id/tv1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"

 android:autolink="web|phone|email"
/>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网