Android Development-Imitation iOS searchbar right Delete button

Source: Internet
Author: User

Android is not like iOS Searchbar and can only write on its own.

Need a. 9 as a background, EditText as a search box, a delete image, a search image (magnifying glass)

<relativelayout Android:id= "@+id/rlsearchframedelete"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"android:gravity= "Center_vertical" > <EditText Android:id= "@+id/etsearch"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_marginright= "10DP"Android:background= "@drawable/search_frame"Android:hint= "Search"Android:paddingleft= "32DP"Android:singleline= "true"android:textsize= "12SP"/> <ImageView Android:id= "@+id/ivdeletetext"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centerinparent= "true"Android:paddingright= "20DP"android:src= "@drawable/delete"android:visibility= "Gone"/> </RelativeLayout>

Listen for input box, the Delete button appears.

Etsearch.addtextchangedlistener (NewTextwatcher () { Public voidOnTextChanged (Charsequence S,intStartintbefore,intcount) {                //TODO auto-generated Method Stub            }             Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) {                //TODO auto-generated Method Stub            }             Public voidaftertextchanged (Editable s) {if(s.length () = = 0) {ivdeletetext.setvisibility (view.gone); } Else{ivdeletetext.setvisibility (view.visible); }            }        });

Delete button click event

Ivdeletetext.setonclicklistener (new  Onclicklistener () {            publicvoid  OnClick (View v) {                etsearch.settext ("");            }        });

Android Development-Imitation iOS searchbar right Delete button

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.