當(dāng)我們?cè)谝粋€(gè)EditText輸入電話或者網(wǎng)址還是Email的時(shí)候,讓Android自動(dòng)判斷,當(dāng)我們輸入的是電話,我們點(diǎn)擊輸入內(nèi)容將調(diào)用打電話程序,當(dāng)我們輸入是網(wǎng)址點(diǎn)擊將打開(kāi)瀏覽器程序.而Linkify很好的解決了這個(gè)問(wèn)題
步驟:
1、布局UI
<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>
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,并自動(dòng)與系統(tǒng)連接
Linkify.addLinks(tv, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS |);
return false;
}
});
}
}
android:autoLink="web|phone|email"
/>
|
新聞熱點(diǎn)
疑難解答
圖片精選