Djedittext.java
/*** Created by Xp.chen on 2016/11/25.*/ Public classDjedittextextendsAppcompatedittext {Private Static Final intDrawable_left = 0; Private Static Final intDrawable_top = 1; Private Static Final intDrawable_right = 2; Private Static Final intDrawable_bottom = 3; Privatedrawable mcleardrawable; PublicDjedittext (Context context) {Super(context); Init (); } PublicDjedittext (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicDjedittext (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Init (); } Private voidinit () {mcleardrawable=getresources (). getdrawable (R.drawable.app_clear_btn_bg_selector); } @Overrideprotected voidOnTextChanged (charsequence text,intStartintLengthbefore,intlengthafter) { Super. ontextchanged (text, start, Lengthbefore, Lengthafter); Setcleariconvisible (Hasfocus ()&& length () > 0); } @Overrideprotected voidOnfocuschanged (BooleanFocusedintdirection, Rect Previouslyfocusedrect) { Super. Onfocuschanged (focused, direction, previouslyfocusedrect); Setcleariconvisible (focused&& length () > 0); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_UP:Drawable drawable=getcompounddrawables () [Drawable_right]; if(Drawable! =NULL&& Event.getx () <= (GetWidth ()-getpaddingright ()) && Event.getx () >= (GetWidth ()-Getpaddingrigh T ()-drawable.getbounds (). Width ())) {SetText (""); } Break; default: Break; } return Super. Ontouchevent (event); } Public voidSetcleariconvisible (Booleanvisible) {Setcompounddrawableswithintrinsicbounds (Getcompounddrawables () [Drawable_left], Getcompounddrawables () [DRAWABLE _top], Visible? Mcleardrawable:NULL, Getcompounddrawables () [Drawable_bottom]); }}
Final effect:
Android-android with delete function edittext