Android edittext Input Box Search implementation

Source: Internet
Author: User

Recently in a small project to use the search function, so on the Internet to find information, now recorded, so as not to forget later. A small example written when learning Android is written on the basis of

Reference Links:

Android implements A-Z alphabetical sorting and filtering search function for the ListView, enabling the kanji to be converted into pinyin

[Android] text box for search and erase effects

----------------------------------------------------------------------------------------

A ListView and Adapter fill data to dynamically change the display of the ListView based on what is entered

Mainactivity:
Private ListView LV; Private list<goods> lists=New arraylist<goods>(); Private EditText Et_search; Private Goodsadapter Adapter; Private Drawable Imgclear;

lv=(ListView) Findviewbyid (r.id.lv_goods); Et_search=(EditText) Findviewbyid (r.id.et_search); et_ Search.addtextchangedlistener (this); // Filter the Search Et_search.setontouchlistener (this) based on the change in input value of the entry box ; imgclear=new Goodsadapter (mainactivity.  this, r.layout.goods_item,lists); Lv.setadapter (adapter);

 Public voidFilterdata (String strdata) {List<Goods> filterlists=NewArraylist<goods>(); //If the input box is empty, all data is displayed        if(Textutils.isempty (strdata)) {filterlists=lists; }Else{//Otherwise, the search results are displayedfilterlists.clear ();  for(Goods list:lists) {String name=List.getname ();            Filterlists.add (list); }} adapter.updateview ( This, filterlists); }    //Filter search based on changes in input values@Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) {        //when the value inside the input box is empty, update to the original list, otherwise filter the data listFilterdata (s.tostring ()); } @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) {} @Override Public voidaftertextchanged (Editable s) {//The delete icon appears when the input box is not empty, and disappears when it is empty        if(Textutils.isempty (s)) {et_search.setcompounddrawableswithintrinsicbounds (NULL,                    NULL, Imgclear,NULL); }    }    //Empty the input box@Override PublicBoolean OnTouch (View V, motioneventEvent) {        Switch(Event. Getaction ()) {         Casemotionevent.action_up:intCurX = (int)Event. GetX (); if(CurX > V.getwidth ()- -&&!Textutils.isempty (Et_search.gettext ())) {Et_search.settext (""); intCacheinputtype = Et_search.getinputtype ();//backup the input typeEt_search.setinputtype (Inputtype.type_null);//Disable Soft inputEt_search.ontouchevent (Event);//Call Native HandlerEt_search.setinputtype (Cacheinputtype);//Restore input type                return true;//consume touch even            }             Break; }        return false; }

Android edittext Input Box Search implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.