Custom edittext with the delete button

Source: Internet
Author: User

With the Clear button, you can add images to the left and right sides. By default, the clear button is added.

Clearing the content is the range of the judgment gesture to click edittext.

The custom edittext code is as follows:

Import android. content. context; import android. graphics. drawable. drawable; import android. text. editable; import android. text. textutils; import android. text. textwatcher; import android. util. attributeset; import android. view. motionevent; import android. view. view; import android. widget. edittext;/*** created by Administrator on 2014/9/15. */public class clearedittext extends edittext implements textwatcher, view. onfocuschangelistener {/*** image resources on both sides */private drawable left, right;
 
        /** 
* You can add clearlistener to clear the data on the following page.
         */ 
Private clearlistener mclearlistener;/*** indicates whether to obtain the focus. By default, no focus */private Boolean hasfocus = false; /*** X coordinate when the finger is lifted */private int xup = 0; private Boolean hasmylistener = false; public interface clearlistener {public void dochange ();} public void setmylistener (clearlistener) {mclearlistener = clearlistener; hasmylistener = true;} public clearedittext (context) {This (context, NUL L);} public clearedittext (context, attributeset attrs) {This (context, attrs, android. r. ATTR. edittextstyle);} public clearedittext (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); initwedgits ();} private void initwedgits () {try {// obtain the drawableleft image. If the drawableleft attribute is not defined in the layout file, this value is left = getcompounddrawables () [0]; // obtain the drawableright image, if D is not defined in the layout file Rawableright property, the value is null right = getcompounddrawables () [2]; initdatas () ;}catch (exception e) {e. printstacktrace () ;}/ *** initialization data */private void initdatas () {try {// display for the first time, hide the delete icon setcompounddrawableswithintrinsicbounds (left, null, null, null); addlisteners ();} catch (exception e) {e. printstacktrace () ;}}/*** add event listener */private void addlisteners () {try {setonfocuschangelistener (this); addt Extchangedlistener (this);} catch (exception e) {e. printstacktrace () ;}@ override public void beforetextchanged (charsequence S, int start, int count, int after) {}@ override public void ontextchanged (charsequence S, int start, int before, int after) {If (hasfocus) {If (textutils. isempty (s) {// if it is null, the delete icon setcompounddrawableswithintrinsicbounds (left, null) is not displayed; If (hasmylistener ){ Mclearlistener. dochange () ;}} else {// if not empty, the delete icon if (null = right) {right = getcompounddrawables () [2];} is displayed. setcompounddrawableswithintrinsicbounds (left, null, right, null) ;}}@ override public Boolean ontouchevent (motionevent event) {try {Switch (event. getaction () {Case motionevent. action_up: // obtain the X coordinate xup = (INT) event raised by the finger when a click is made. getx (); // The distance from the clicked coordinates to the right of the current input box is less than or equal to getcompoundpaddingright () When you click the delete icon // getcompoundpaddingright (): returns the right padding of the view, plus space for the right drawable if any. if (getwidth ()-xup) <= getcompoundpaddingright () {If (! Textutils. isempty (gettext (). tostring () {settext ("") ;}} break; default: Break ;}} catch (exception e) {e. printstacktrace ();} return Super. ontouchevent (event) ;}@ override public void aftertextchanged (editable s) {}@ override public void onfocuschange (view V, Boolean hasfocus) {try {This. hasfocus = hasfocus;} catch (exception e) {e. printstacktrace ();}}}

 

See Implementation in layout:

<COM. tuge. export Android: layout_toleftof = "@ ID/search_button" Android: layout_width = "match_parent" Android: layout_height = "40dp" Android: layout_centervertical = "true" Android: layout_marginleft = "8dp" Android: layout_marginright = "8dp" Android: paddingleft = "8dp" Android: paddingright = "8dp" Android: hint = "Enter the waybill number or booking number" Android: drawablepadding = "8dp" Android: textsize = "16sp" Android: textcolorhint = "# ffa9a9a9" Android: singleline = "true" Android: Id = "@ + ID/search_edittext" Android: drawableleft = "@ drawable/ico_search_small" Android: drawableright = "@ drawable/text_clear_selector" Android: Background = "@ drawable/search_background"/>

Ico_search_small:

Text_clear_selector:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/ico_text_clear"/>    <item android:drawable="@drawable/ico_text_clear_click"          android:state_pressed="true"/>    <item   android:state_focused="true" android:drawable="@drawable/ico_text_clear_click"/></selector>

Ico_text_clear_click:

 

Finally, you can get it in the code.

Custom edittext with the 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.