Android 自動判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用
當(dāng)我們在一個(gè)EditText輸入電話或者網(wǎng)址還是Email的時(shí)候,讓Android自動判斷,當(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,并自動與系統(tǒng)連接
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"
/>
相關(guān)文章
Android編程使用加速度傳感器實(shí)現(xiàn)搖一搖功能及優(yōu)化的方法詳解
這篇文章主要介紹了Android編程使用加速度傳感器實(shí)現(xiàn)搖一搖功能及優(yōu)化的方法,結(jié)合實(shí)例形式分析了Android傳感器的調(diào)用方法、參數(shù)含義及具體使用技巧,需要的朋友可以參考下2017-08-08
怎樣才能導(dǎo)入別人的android項(xiàng)目不再報(bào)錯(cuò)
每次看到好的項(xiàng)目都想拿過來看看源碼,可是導(dǎo)入以后各種報(bào)錯(cuò)怎么辦?源碼有問題嗎?有這種可能,但更多的可能性是你沒有正確導(dǎo)入這個(gè)項(xiàng)目2021-08-08
Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對話框的方法
這篇文章主要介紹了Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對話框的方法,涉及設(shè)置監(jiān)聽的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
appium運(yùn)行各種坑爹報(bào)錯(cuò)問題及解決方法【推薦】
這篇文章主要介紹了 appium運(yùn)行各種坑爹報(bào)錯(cuò)問題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊功能(在之前的微信頁面基礎(chǔ)之上)
這篇文章主要介紹了Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊(在之前的微信頁面基礎(chǔ)之上),本文通過截圖實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2020-03-03
Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解
這篇文章主要介紹了Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
AndroidStudio更新出現(xiàn)Refreshing ''xxx'' Gradle Project狀態(tài)解決辦法
這篇文章主要介紹了AndroidStudio更新出現(xiàn)Refreshing 'xxx' Gradle Project狀態(tài)解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03

