html5中文学习网

您的位置: 首页 > android » 正文

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

[ ] 已经帮助:人解决问题

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

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助