现在的位置: 首页 > 综合 > 正文

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

2013年03月11日 ⁄ 综合 ⁄ 共 1352字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.