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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解Android 自动判断是电话,网址,EMAIL方法之Linkify的使用的相关内容吗,在本文为您仔细讲解android linkify 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,linkify,下面大家一起来学习吧。

当我们在一个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"
/>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们