1. Configure the auto-prompt control in Main.xml:
<AutocompletetextviewAndroid:id= "@+id/autotv_searchresult"Android:layout_width= "280dip"Android:layout_height= "35dip"android:layout_centerinparent= "true"Android:background= "#00000000"Android:completionthreshold= "1"android:imeoptions= "Actionsearch"Android:dropdownheight= "Wrap_content"Android:dropdownverticaloffset= "10dip"Android:hint= "Search for songs, singers, Music Box names ..."Android:singleline= "true" />
Note:android:dropdownverticaloffset= "10dip" is an attribute that can be used to open a distance between the information of the hint and the border, usually
2. Create a layout for the search bar. xml:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/brandname"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" /> <TextViewAndroid:id= "@+id/searchtext"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:visibility= "Gone" /></LinearLayout>
Note: android:visibility= "Gone" This is the key code to enter the English alphabet with Chinese hints
3 . Define 2 arrays and assign values to the array at the beginning of the program:
//All search Information Public StaticarraylistNewArraylist(); //Some of the search information Public StaticarraylistNewArraylist(); Private voidaddallsearch (string input, string output) {HashMap<string, string>item; Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", input); Item.put ("Brandname", output); List_searchall.add (item); } Private voidAddallsearchitems () {Addallsearch ("Xbh Xuebuhui xbh", "Learn Not"); Addallsearch ("YXJ Yixiangji YXJ", "different Want to Remember"); Addallsearch ("XWZ Xiaowangzi Wangzi", "The Little Prince's drifting bottle"); Addallsearch ("Xygzj xiayige xygzj", "Next Self"); Addallsearch ("Nyfz Nuoyafanzhou Nyfz", "Noah's Ark"); Addallsearch ("XT Xintiao XT", "Heartbeat"); Addallsearch ("Qs Qinshang Qs", "Qin injury"); Addallsearch ("Aqsfyr Aiqinshifou aqsf", "Love Is still"); Addallsearch ("Nzfrsm Nizaifanrao nzfrsm", "What are you bothering?"); Addallsearch ("Wmhghd womenghuigenghaode Wmhgh", "we'll be better."); } Private voidaddsomesearch (string input, string output) {HashMap<string, string>item; Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", input); Item.put ("Brandname", output); List_searchsome.add (item); } Private voidAddsomesearchitems () {//AAddsomesearch ("aqmm Aiqingmaima", "Love Business"); Addsomesearch ("Awnjbbw Aiwonijiubbw", "Love Me, you hug me."); Addsomesearch ("Abzjffs aibuzaijiuffs", "Love is not let go"); //BAddsomesearch ("Bcys Banchengyansha", "Half City Smoke Sand"); Addsomesearch ("Bfsdla Bufengshoudelianai", "Don't Break Up in love"); Addsomesearch ("Bygsmm Buyaogaoshumama", "Don't Tell Mommy."); }
4. Get the control ID and create the Simpleadapter adapter:
Private Autocompletetextview Autotv_search; Private = (autocompletetextview) Findviewbyid (R.id.autotv_search);
Note: The instantiation of the adapter can be monitored
5. Setting the Autocompletetextview monitoring
Private voidAutosearchlisten () {Autotv_search.setonkeylistener (NewView.onkeylistener () {//Add soft keyboard events (let the soft keyboard have a search icon)@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { //TODO auto-generated Method Stub if(KeyCode = =keyevent.keycode_enter) { if(event.getaction () = =keyevent.action_up) {Inputmethodmanager IMM=(Inputmethodmanager) v. GetContext (). Getsystemservice (Context.input_method_service); if(Imm.isactive ()) {Imm.hidesoftinputfromwindow (v. Getapplicationwindowtoken ()),0); } return true; } } return false; }}); Note:if(KeyCode = =keyevent.keycode_enter) { if(event.getaction () = =keyevent.action_up) {It is triggered when the ENTER key comes up, otherwise it will be called 2 times, once by pressing the next bounce Autotv_search.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub if(Autotv_search.gettext (). toString (). Equals ("") ) {Madapter=NewSimpleadapter (searchactivity. This, Homeactivity.list_searchall, R.layout.searchlist,Newstring[] {"Brandsearchtext", "brandname" }, New int[] {r.id.tv_searchtext, r.id.tv_brandname}); Autotv_search.setadapter (Madapter); Autotv_search.showdropdown (); } } }); Note: Click on the Search box trigger, remember the parameters are to correspond, you understand ... autotv_search.showdropdown (); manual pop-up hint Autotv_search.setonfocuschangelistener (NewOnfocuschangelistener () {@Override Public voidOnfocuschange (View V,BooleanHasfocus) { if(Hasfocus & Autotv_search.gettext (). toString (). Equals ("") ) {Madapter=NewSimpleadapter (searchactivity. This, Homeactivity.list_searchall, R.layout.searchlist,Newstring[] {"Brandsearchtext", "brandname" }, New int[] {r.id.tv_searchtext, r.id.tv_brandname}); Autotv_search.setadapter (Madapter); Autotv_search.showdropdown (); } } }); //Click on the entry to listenAutotv_search.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {TextView TV=(TextView) View.findviewbyid (r.id.tv_brandname); Autotv_search.settext (Tv.gettext (). toString ()); Autotv_search.setselection (Autotv_search.gettext (). toString (). Length ()); } }); //Textwatcher is a key place for this application ... Thanks to the tips of the children's shoes, this is called when the character is transformedtextwatcher TW=NewTextwatcher () {@Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { //TODO auto-generated Method Stub if(Autotv_search.gettext (). toString (). Equals ("") ) {Madapter=NewSimpleadapter (searchactivity. This, Homeactivity.list_searchall, R.layout.searchlist,Newstring[] {"Brandsearchtext", "brandname" }, New int[] {r.id.tv_searchtext, r.id.tv_brandname}); Autotv_search.setadapter (Madapter); Autotv_search.showdropdown (); } Else if(Autotv_search.gettext (). toString (). Length () < 3) {Madapter=NewSimpleadapter (searchactivity. This, Homeactivity.list_searchsome, R.layout.searchlist,Newstring[] {"Brandsearchtext", "brandname" }, New int[] {r.id.tv_searchtext, r.id.tv_brandname}); Autotv_search.setadapter (Madapter); //madapter.notifydatasetchanged ();}} @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { //TODO auto-generated Method Stub} @Override Public voidaftertextchanged (Editable s) {//TODO auto-generated Method Stub if(Autotv_search.gettext (). toString (). Equals ("") ) {Madapter=NewSimpleadapter (searchactivity. This, Homeactivity.list_searchall, R.layout.searchlist,Newstring[] {"Brandsearchtext", "brandname" }, New int[] {r.id.tv_searchtext, r.id.tv_brandname}); Autotv_search.setadapter (Madapter); Autotv_search.showdropdown (); } } }; //add text to change the listenerAutotv_search.addtextchangedlistener (TW); }
Conclusion: This way has been changing the adapter method does not know whether the performance of a certain problem ... Testing on the real machine is still very fast, there are a lot of programs to optimize the place, if there is a better way to hope pointing ah ...