AutoCompleteTextView是一個(gè)具有自動(dòng)補(bǔ)全功能的EditView,當(dāng)用戶輸入數(shù)據(jù)后,AutoCompleteTextView就會(huì)將用戶輸入的數(shù)據(jù)與他自己的adapter中的數(shù)據(jù)對(duì)比,如果用戶數(shù)據(jù)與adapter中的某條數(shù)據(jù)的開始部分完全匹配,那么adapter中的這條數(shù)據(jù)就會(huì)出現(xiàn)在下拉提示框中。
其常用屬性定義如下
<AutoCompleteTextView android:id="@+id/mp002_top_place_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:completionThreshold="1" android:layout_marginTop="5dp" ></AutoCompleteTextView>
其中android:completionThreshold定義了從第幾個(gè)字符開始顯示候補(bǔ)列表。
默認(rèn)值為2。
使用例:
AutoCompleteTextView mPlace = (AutoCompleteTextView)findViewById(R.id.mp002_top_place_input);ArrayList<String> result = new ArrayList<String>();result.add("1111111");result.add("1222222");mPlace.setAdapter(new ArrayAdapter<String>( MP002TopActivity.this, android.R.layout.simple_dropdown_item_1line, result));
局限性是completionThreshold設(shè)定的最小值是1,
小于1的情況下,會(huì)默認(rèn)變成1。
所以要在不輸入任何字符的條件下顯示候補(bǔ)列表,
就必須重載AutoCompleteTextView這個(gè)控件。
public class MyAutoCompleteTextView extends AutoCompleteTextView{ public MyAutoCompleteTextView(Context context) { super(context); } public MyAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean enoughToFilter() { return true; } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); performFiltering(getText(), KeyEvent.KEYCODE_UNKNOWN); }}enoughToFilter()是判斷輸入文字列長(zhǎng)度是否滿足現(xiàn)實(shí)候補(bǔ)列表的要求的方法。
onFocusChanged()是當(dāng)控件獲得焦點(diǎn)時(shí)讓其顯示候補(bǔ)列表。

使用AutoCompleteTextView實(shí)現(xiàn)郵箱地址補(bǔ)全
例如:adapter中有3條數(shù)據(jù)“abc”,“hjk”,“abd”,而用戶輸入“ab”,那么下拉提示框中將會(huì)出現(xiàn)“abc”和“abd”。(AutoCompleteTextView默認(rèn)在用戶輸入兩個(gè)字符之后才提示,可以通過setThreshold(1)來將它設(shè)置為用戶輸入1個(gè)字符后就開始提示)
AutoCompleteTextView在匹配用戶輸入數(shù)據(jù)時(shí),會(huì)調(diào)用performFiltering方法,將用戶數(shù)據(jù)傳入,并調(diào)用adapter的filter來處理。
因?yàn)楫?dāng)用戶選中下拉列表中的某一項(xiàng)時(shí),AutoCompleteTextView會(huì)使用該項(xiàng)對(duì)應(yīng)的adapter中的數(shù)據(jù)來填充文本域,這與我們這邊的需求不太相同,因?yàn)槲覀兊腶dapter中只有類似于“@163.com”的email地址后綴,下拉框中的數(shù)據(jù)是我們將用戶輸入和adapter中的數(shù)據(jù)拼接而成的。因此我們需要重寫replaceText方法,以使AutoCompleteTextView來在用戶選中某一項(xiàng)時(shí),用我們指定的文本來填充文本域。
然后我們需要為AutoCompleteTextView設(shè)置OnFocusChangeListener來在用戶移開焦點(diǎn)后,進(jìn)行email地址格式檢查,并且在再次獲得焦點(diǎn)后重啟提示功能。
代碼如下:(EmailAutoCompleteTextView.java)
public class EmailAutoCompleteTextView extends AutoCompleteTextView { private static final String TAG = "EmailAutoCompleteTextView"; private String[] emailSufixs = new String[] { "@163.com", "@gmail.com", "@hotmail.com" }; public EmailAutoCompleteTextView(Context context) { super(context); init(context); } public EmailAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public EmailAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public void setAdapterString(String[] es) { if(es != null && es.length > 0) this.emailSufixs = es; } private void init(final Context context) { //adapter中使用默認(rèn)的emailSufixs中的數(shù)據(jù),可以通過setAdapterString來更改 this.setAdapter(new EmailAutoCompleteAdapter(context, R.layout.auto_complete_item, emailSufixs)); //使得在輸入1個(gè)字符之后便開啟自動(dòng)完成 this.setThreshold(1); this.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { String text = EmailAutoCompleteTextView.this.getText().toString(); //當(dāng)該文本域重新獲得焦點(diǎn)后,重啟自動(dòng)完成 if(!"".equals(text)) performFiltering(text, 0); } else { //當(dāng)文本域丟失焦點(diǎn)后,檢查輸入email地址的格式 EmailAutoCompleteTextView ev = (EmailAutoCompleteTextView) v; String text = ev.getText().toString(); //這里正則寫的有點(diǎn)粗暴:) if(text != null && text.matches("^[a-zA-Z0-9_]+@[a-zA-Z0-9]+//.[a-zA-Z0-9]+$")) { Toast to = new Toast(context); ImageView i = new ImageView(context); i.setBackgroundResource(R.drawable.img_success); to.setView(i); to.show(); } else { Toast toast = Toast.makeText(context, "郵件地址格式不正確", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 50); toast.show(); } } } }); } @Override protected void replaceText(CharSequence text) { //當(dāng)我們?cè)谙吕蛑羞x擇一項(xiàng)時(shí),android會(huì)默認(rèn)使用AutoCompleteTextView中Adapter里的文本來填充文本域 //因?yàn)檫@里Adapter中只是存了常用email的后綴 //因此要重新replace邏輯,將用戶輸入的部分與后綴合并 Log.i(TAG + " replaceText", text.toString()); String t = this.getText().toString(); int index = t.indexOf("@"); if(index != -1) t = t.substring(0, index); super.replaceText(t + text); } @Override protected void performFiltering(CharSequence text, int keyCode) { //該方法會(huì)在用戶輸入文本之后調(diào)用,將已輸入的文本與adapter中的數(shù)據(jù)對(duì)比,若它匹配 //adapter中數(shù)據(jù)的前半部分,那么adapter中的這條數(shù)據(jù)將會(huì)在下拉框中出現(xiàn) Log.i(TAG + " performFiltering", text.toString() + " " + keyCode); String t = text.toString(); //因?yàn)橛脩糨斎豚]箱時(shí),都是以字母,數(shù)字開始,而我們的adapter中只會(huì)提供以類似于"@163.com" //的郵箱后綴,因此在調(diào)用super.performFiltering時(shí),傳入的一定是以"@"開頭的字符串 int index = t.indexOf("@"); if(index == -1) { if(t.matches("^[a-zA-Z0-9_]+$")) { super.performFiltering("@", keyCode); } else this.dismissDropDown();//當(dāng)用戶中途輸入非法字符時(shí),關(guān)閉下拉提示框 } else { super.performFiltering(t.substring(index), keyCode); } } private class EmailAutoCompleteAdapter extends ArrayAdapter<String> { public EmailAutoCompleteAdapter(Context context, int textViewResourceId, String[] email_s) { super(context, textViewResourceId, email_s); } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i(TAG, "in GetView"); View v = convertView; if (v == null) v = LayoutInflater.from(getContext()).inflate( R.layout.auto_complete_item, null); TextView tv = (TextView) v.findViewById(R.id.tv); String t = EmailAutoCompleteTextView.this.getText().toString(); int index = t.indexOf("@"); if(index != -1) t = t.substring(0, index); //將用戶輸入的文本與adapter中的email后綴拼接后,在下拉框中顯示 tv.setText(t + getItem(position)); Log.i(TAG, tv.getText().toString()); return v; } }}activity的xml文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.testautocompletetextview.EmailAutoCompleteTextView android:id="@+id/act" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email Address" android:textColor="@color/black" /> <!-- 用于測(cè)試移開焦點(diǎn) --> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:drawableLeft="@drawable/amount_selected" /></LinearLayout>下拉提示框中每一項(xiàng)(TextView)的xml:<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv" android:padding="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" />
提示截圖:



新聞熱點(diǎn)
疑難解答
圖片精選