Android implements the EditText sample code with the Delete button _android

Source: Internet
Author: User
Tags gety touch

First, look at the effect

This is an input text box with a Delete button, you need to create a new class to inherit from the EditText, post the code first, and then explain:

The sample code is as follows:

public class Edittextwithdel extends EditText {private final static String TAG = "Edittextwithdel";
 Private drawable imginable;

 Private context Mcontext;
 Public Edittextwithdel {This (context, NULL, 0);
 Public Edittextwithdel (context, AttributeSet attrs) {This (context, attrs, 0); 
  Public Edittextwithdel (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
  Mcontext = context;
 Init (); private void Init () {imginable = Mcontext.getresources (). Getdrawable (Android).
  R.drawable.ic_delete);  Addtextchangedlistener (New Textwatcher () {@Override public void beforetextchanged (charsequence charsequence, int i,

   int i1, int i2) {} @Override public void ontextchanged (charsequence charsequence, int i, int i1, int i2) {
   @Override public void aftertextchanged (Editable Editable) {setdrawable ();

  }
  });
 Setdrawable ();
  }//Set delete picture private void setdrawable () {if (length () < 1) {setcompounddrawableswithintrinsicbounds (null, NULL, NULL, NULL);
  else {setcompounddrawableswithintrinsicbounds (null, NULL, imginable, NULL); }//Process deletion @Override public boolean ontouchevent (Motionevent event) {if imginable!= null && EVENT.G
   Etaction () = = motionevent.action_up) {int eventx = (int) event.getrawx ();
   int eventy = (int) Event.getrawy ();
   LOG.D (TAG, "(" + Eventx + "," + Eventy + "));
   Rect Rect = new Rect ();
   Getglobalvisiblerect (rect);
   Rect.left = rect.right-70;
   LOG.D (TAG, rect.tostring ());
   if (Rect.contains (Eventx, Eventy)) {SetText ("");
 } return Super.ontouchevent (event);
 @Override protected void Finalize () throws Throwable {super.finalize (); }
}

Explained as follows

First look at setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) the name of the thief long method, although the name is very long, in fact, this method used and simple, is to set the picture on the left and right, this dome only need to set the right side of the line, you can see the effect of all settings

Then there is also listening to the touch, here to say getRawX() and getX() the difference, getRawX() or the getRawY() method is the screen for reference, getX() and the getY() method is a container for reference, so the value may be different. After using getGlobalVisibleRect() method to get the location of the view, stored in the Rect, here in the upper left corner of the screen as the starting point, so the front is the Getraw method.

Of course, you can also use the Getlocalvisiblerect method, which starts with the upper-left corner of the view, so you have to use getX() and getY() method to get the X and Y values of the touch points in this way.

Summarize

This is the full content of this article, I hope the content of this article for your Android developers can help, if you have questions you can message exchange.

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.