因為項目需要,要寫一個郵箱自動補(bǔ)全的edittext,剛開始考慮使用autocompletetextview來實現(xiàn),但是滿足不到需求官方組件太low了。。。
先來介紹下autocompletetextview 的使用:
activity
import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.arrayadapter; import android.widget.autocompletetextview; public class mainactivity extends appcompatactivity { private static final string[] countries = new string[]{ "belgium", "france", "italy", "germany", "spain" }; private autocompletetextview mautocompletetextview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mautocompletetextview = (autocompletetextview) findviewbyid(r.id.autocompletetextview); arrayadapter<string> adapter = new arrayadapter(this, android.r.layout.simple_dropdown_item_1line, countries); mautocompletetextview.setadapter(adapter); } }
xml
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="autocompletetextview實現(xiàn)" android:textcolor="@android:color/black" /> <autocompletetextview android:id="@ id/autocompletetextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionthreshold="1"/> </linearlayout>
使用方法很簡單,獲取到組件然后設(shè)置一個彈出的adapter就能完成一個自動提示。
但是這個組件有幾個特性不是很滿足我們的需求。
1.默認(rèn)是第二個字母開始匹配
2.整體內(nèi)容匹配模式 belgium 我們只能輸入b ,be,bel等才會匹配
注:郵箱格式為123456@xx.com,整體內(nèi)容肯定不行。
下面我們又看到一個multiautocompletetextview組件,來看下multiautocompletetextview能否滿足我們的需求
import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.multiautocompletetextview; public class mainactivity extends appcompatactivity { private static final string[] countries = new string[]{ "belgium", "france", "italy", "germany", "spain" }; private autocompletetextview mautocompletetextview; private multiautocompletetextview mmultiautocompletetextview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mautocompletetextview = (autocompletetextview) findviewbyid(r.id.autocompletetextview); mmultiautocompletetextview = (multiautocompletetextview) findviewbyid(r.id.multiautocompletetextview); arrayadapter<string> adapter = new arrayadapter(this, android.r.layout.simple_dropdown_item_1line, countries); mautocompletetextview.setadapter(adapter); mmultiautocompletetextview.setadapter(adapter); mmultiautocompletetextview.settokenizer(new multiautocompletetextview.commatokenizer()); } }
xml
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="autocompletetextview實現(xiàn)"