android - EditText & TextChangeListener -


i need yours help. have edittext field, acts search field searching thought many items in list. i'm use aftertextchanged(editable s) method of textwatcher, it's not perfect me. times after fast input , erase next search process involve not text inputed user. reason in long search process , can't make shorter. in case need know, wnen user ends input @ all, aftertextchanged() handle every sign change. appreciate ideas. thanks!

i'm guessing you're using textwatcher because want make live searches. in case can't know when user has finished input can limit frequency of searches.

here's sample code:

searchinput.addtextchangedlistener(new textwatcher() {     handler handler = new handler();     runnable delayedaction = null;      @override     public void ontextchanged( charsequence s, int start, int before, int count)     {}      @override     public void beforetextchanged( charsequence s, int start, int count, int after)     {}      @override     public void aftertextchanged( final editable s)     {         //cancel previous search if         if (delayedaction != null)         {             handler.removecallbacks(delayedaction);         }          //define new search         delayedaction = new runnable()         {             @override             public void run()             {                 //start search                 startsearch(s.tostring());             }         };          //delay new search 1 second         handler.postdelayed(delayedaction, 1000);     } }); 

the way know if input has ended user press enter or search button or something. can listen event following code:

searchinput.setoneditoractionlistener(new oneditoractionlistener() {      @override     public boolean oneditoraction( textview v, int actionid, keyevent event)     {         switch (actionid)         {         case editorinfo.ime_action_search:             //get input string , start search             string searchstring = v.gettext().tostring();             startsearch(searchstring);             break;         default:             break;         }         return false;     } }); 

just make sure add android:imeoptions="actionsearch" edittext in layout file.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -