Android編程實現(xiàn)輸入框動態(tài)自動提示功能
本文實例講述了Android編程實現(xiàn)輸入框動態(tài)自動提示功能。分享給大家供大家參考,具體如下:
關(guān)于AutoCompleteTextView
的使用,我想大家并不陌生,對其設(shè)定上Adapter后系統(tǒng)便能自己識別與匹配了。近期 一個項目中,需要做到匹配通迅錄中的電話號碼和聯(lián)系人,由于通迅錄中數(shù)據(jù)量大,所以把所有的數(shù)據(jù)在自己提示之前就查詢出來并加入到 AutoCompleteTextView中是不現(xiàn)實的,所以我們可以使用cursor
來動態(tài)加載AutoCompleteTextView的數(shù)據(jù),從而 實現(xiàn)時時搜索提示,要實現(xiàn)動態(tài)加載,只用重寫一個類繼承于CursorAdapter
,然后設(shè)定在AutoCompleteTextView上就行了。
AutoCompleteTextView editNumber = (AutoCompleteTextView)findViewById(R.id.edit_number); Cursor cursor = getContentResolver()(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); ContactListAdapter listAdapter = new ContactListAdapter(this, cursor); editNumber.setAdapter(listAdapter);
ContactListAdapter.java中的核心代碼如下:
重寫newView方法
public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context); final View view = (View)inflater.inflate( R.layout.auto_complete, parent, false); TextView txtName = (TextView)view.findViewById(R.id.txt_name); txtName.setText(cursor.getString(0)); TextView txtNumber = (TextView)view.findViewById(R.id.txt_number); txtNumber.setText(cursor.getString(1)); TextView txtType = (TextView)view.findViewById(R.id.txt_type); String[] arrType = SmsConstant.ARR_CONTACTS_TYPE; if(cursor.getint(2) > 3) { txtType.setText(arrType[0]); } else { txtType.setText(arrType[cursor.getint(2)]); } return view; }
重寫bindView方法,
public void bindView(View view, Context context, Cursor cursor) { TextView txtName = (TextView)view.findViewById(R.id.txt_name); txtName.setText(cursor.getString(0)); TextView txtNumber = (TextView)view.findViewById(R.id.txt_number); txtNumber.setText(cursor.getString(1)); TextView txtType = (TextView)view.findViewById(R.id.txt_type); String[] arrType = SmsConstant.ARR_CONTACTS_TYPE; if(cursor.getint(2) > 3) { txtType.setText(arrType[0]); } else { txtType.setText(arrType[cursor.getint(2)]); } }
點擊彈出的Listview列表后的返回值:
public String convertToString(Cursor cursor) {}
執(zhí)行搜索的sql語句,返回一個Cursor加載到彈出的Listview上
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {}
在此所返回的Cursor結(jié)果,會全部顯示在彈出提示上,無需再次過慮。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android EditText設(shè)置邊框的操作方法
這篇文章主要介紹了Android EditText設(shè)置邊框,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12Android 3D旋轉(zhuǎn)動畫效果實現(xiàn)分解
如何實現(xiàn)View的3D旋轉(zhuǎn)效果,實現(xiàn)的主要原理就是圍繞Y軸旋轉(zhuǎn),同時在Z軸方面上有一個深入的縮放,具體實現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06Android開發(fā)之ViewSwitcher用法實例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-02-02Kotlin開發(fā)實戰(zhàn)之hello world
這篇文章主要為大家詳細介紹了Kotlin開發(fā)實戰(zhàn)之hello world的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05