Android 自動(dòng)判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用
當(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)擊將打開瀏覽器程序.而Linkify很好的解決了這個(gè)問題
步驟:
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中實(shí)現(xiàn)
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,并自動(dòng)與系統(tǒng)連接
Linkify.addLinks(tv, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS |);
return false;
}
});
}
}
OK!簡(jiǎn)便方法:在TextView中如下申明!
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web|phone|email"
/>
相關(guān)文章
Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
這篇文章主要介紹了Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-01-01Android用代碼獲取手機(jī)root之后的最高權(quán)限
機(jī)得root之后通過代碼可以獲得最高權(quán)限如果沒有root的話請(qǐng)不要往下看,毫無(wú)意義,root之后的朋友可以參考下本文或許有意想不到的收獲2013-03-03Android DaggerActivityComponent錯(cuò)誤解決辦法詳解
這篇文章主要介紹了Android DaggerActivityComponent錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2017-05-05Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
這篇文章主要為大家詳細(xì)介紹了Android客戶端實(shí)現(xiàn)注冊(cè)、登錄代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09RecyclerView無(wú)限循環(huán)效果實(shí)現(xiàn)及示例解析
這篇文章主要為大家介紹了RecyclerView無(wú)限循環(huán)效果實(shí)現(xiàn)及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android自定義控件實(shí)現(xiàn)可左右滑動(dòng)的導(dǎo)航條
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)可左右滑動(dòng)的導(dǎo)航條,能響應(yīng)快速滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android 中對(duì)于圖片的內(nèi)存優(yōu)化方法
Android 中對(duì)于圖片的內(nèi)存優(yōu)化方法,需要的朋友可以參考一下2013-03-03