Android編程實(shí)現(xiàn)輸入框動(dòng)態(tài)自動(dòng)提示功能
本文實(shí)例講述了Android編程實(shí)現(xiàn)輸入框動(dòng)態(tài)自動(dòng)提示功能。分享給大家供大家參考,具體如下:
關(guān)于AutoCompleteTextView
的使用,我想大家并不陌生,對(duì)其設(shè)定上Adapter后系統(tǒng)便能自己識(shí)別與匹配了。近期 一個(gè)項(xiàng)目中,需要做到匹配通迅錄中的電話號(hào)碼和聯(lián)系人,由于通迅錄中數(shù)據(jù)量大,所以把所有的數(shù)據(jù)在自己提示之前就查詢出來(lái)并加入到 AutoCompleteTextView中是不現(xiàn)實(shí)的,所以我們可以使用cursor
來(lái)動(dòng)態(tài)加載AutoCompleteTextView的數(shù)據(jù),從而 實(shí)現(xiàn)時(shí)時(shí)搜索提示,要實(shí)現(xiàn)動(dòng)態(tài)加載,只用重寫一個(gè)類繼承于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)]); } }
點(diǎn)擊彈出的Listview列表后的返回值:
public String convertToString(Cursor cursor) {}
執(zhí)行搜索的sql語(yǔ)句,返回一個(gè)Cursor加載到彈出的Listview上
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {}
在此所返回的Cursor結(jié)果,會(huì)全部顯示在彈出提示上,無(wú)需再次過(guò)慮。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Flutter移動(dòng)端進(jìn)行多渠道打包發(fā)布的全過(guò)程
在使用flutter開發(fā)的過(guò)程中,需要根據(jù)不同的環(huán)境,不同的包名來(lái)打包,下面這篇文章主要給大家介紹了關(guān)于Flutter移動(dòng)端進(jìn)行多渠道打包發(fā)布的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Android EditText設(shè)置邊框的操作方法
這篇文章主要介紹了Android EditText設(shè)置邊框,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12Android編程實(shí)現(xiàn)自定義漸變顏色效果詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義漸變顏色效果,結(jié)合具體實(shí)例形式分析了Android基于xml及代碼定義來(lái)實(shí)現(xiàn)顏色漸變的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-08-08Android 3D旋轉(zhuǎn)動(dòng)畫效果實(shí)現(xiàn)分解
如何實(shí)現(xiàn)View的3D旋轉(zhuǎn)效果,實(shí)現(xiàn)的主要原理就是圍繞Y軸旋轉(zhuǎn),同時(shí)在Z軸方面上有一個(gè)深入的縮放,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06Android開發(fā)之ViewSwitcher用法實(shí)例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實(shí)例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02Kotlin開發(fā)實(shí)戰(zhàn)之hello world
這篇文章主要為大家詳細(xì)介紹了Kotlin開發(fā)實(shí)戰(zhàn)之hello world的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05