Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 自动判断是电话,网址,EMAIL方法之Linkify的使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

步骤:

1、布局UI@H_502_14@

@H_502_14@<LinearLayout xmlns:android="http://scheR_352_11845@as.android.com/apk/res/android"@H_502_14@android:layout_width="match_parent"@H_502_14@android:layout_height="match_parent"@H_502_14@android:orientation="vertical" >

<TextView@H_502_14@android:id="@+id/tv"@H_502_14@android:layout_width="match_parent"@H_502_14@android:layout_height="wrap_content" />

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

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

</LinearLayout>@H_502_14@

@H_502_14@2、在MainActivity中实现@H_502_14@
@H_502_14@public class MainActivity extends Activity {

 private TextView tv;@H_502_14@ private EditText et;@H_502_14@ @Override@H_502_14@ protected void onCreate(Bundle savedInstanceStatE) {@H_502_14@  super.onCreate(savedInstanceStatE);@H_502_14@  setContentView(R.layout.activity_main);

  tv = (TextView) findViewById(R.id.tv1);@H_502_14@  et = (EditText) findViewById(R.id.et);@H_502_14@  et.setOnKeyListener(new OnKeyListener() {@H_502_14@   @Override@H_502_14@   public Boolean onKey(View v,int keyCode,KeyEvent event) {@H_502_14@    tv.setText(et.getText());@H_502_14@    // 判断输入的是URL还是EMAIL还是PHONEnumbER,并自动与系统连接@H_502_14@    Linkify.addLinks(tv,Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_numbERS |);@H_502_14@    return false;@H_502_14@   }@H_502_14@  });@H_502_14@ }@H_502_14@}@H_502_14@

@H_502_14@OK!简便方法:在TextView中如下申明!@H_502_14@@H_502_14@<TextView@H_502_14@ android:id="@+id/tv1"@H_502_14@ android:layout_width="fill_parent"@H_502_14@ android:layout_height="wrap_content"

 android:autoLink="web|phone|email"@H_502_14@/>

大佬总结

以上是大佬教程为你收集整理的Android 自动判断是电话,网址,EMAIL方法之Linkify的使用全部内容,希望文章能够帮你解决Android 自动判断是电话,网址,EMAIL方法之Linkify的使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。