Android Studio EditText Click the icon to clear the text content of the instance resolution _android

Source: Internet
Author: User
Tags xmlns

  This article is after the custom EditText style function enhancement, has the very big reference to the actual application project, the interested friend may step on the previous article, "The Android studion custom EditText style". Specific clear EditText text content function code is as follows:

Package Com.liheng;
Import Android.content.Context;
Import Android.graphics.Rect;
Import android.graphics.drawable.Drawable;
Import Android.support.v4.content.ContextCompat;
Import android.text.Editable;
Import Android.text.TextWatcher;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.widget.EditText;
Import MYSELF.MYAPPLICATION.R; /** * First Step: * Create a class, and inherit EditText this class, implementation of the three constructors of the parent class * We only use implementation with a parameter, two parameters, three parameters of the constructor, with four parameters for the moment regardless of the * * public class Myedittext extends E Dittext {/** * Second step: * Declare 3 variables: Two picture objects (the value of the variable is obtained by instantiating the object, * in the Java world, except the basic data type and static member is not an object, * everything else is an object. Class is also an object, class is an object of class, the picture is D
Rawable object) * 1. When the EditText text content is empty, the right flush icon should be gray, and clicking is no effect of * 2. When the EditText text content is not empty, the right empty icon should be blue, click, empty edittext text content
* 3. Context object/private drawable imageblue;
Private drawable Imagegray;
Private context Mycontext; /** * Three construction methods for implementing the EditText parent class * These three methods must invoke the custom initialization function init () method */Public Myedittext (context) {super (context); Init (conte
XT); Public Myedittext, AttributeSet aTtrs) {Super (context, attrs); init (context);} public Myedittext (context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
Init (context); 
/** * Initialization method: Three global variables used to initialize the declaration: Imageblue,imagegray,mycontext * and be responsible for monitoring changes to edittext text content * * private void init (context context) {
This.mycontext = context; /** * Get Picture resources: * The first way: (Obsolete, not recommended, * should also note that r file imported packages should be under their own project package, * because the picture resources in their own project directory): * Imageblue = Mycontext.getresources (). Getdra
Wable (R.drawable.delete);
* * The second way: (user recommended, the project will be an error?)
* Call Getdrawable () method with two parameters. The second parameter is set to NULL * Imageblue = Mycontext.getresources (). getdrawable (R.drawable.delete, NULL);
* * The Third Way: (Google official recommended), mycontext for their own declaration of the context of the object * Imageblue = contextcompat.getdrawable (Mycontext, r.drawable.delete);
* * Imageblue = contextcompat.getdrawable (Mycontext, r.drawable.delete);
Imagegray = contextcompat.getdrawable (Mycontext, R.drawable.delete_gray); /** * Sets the text listener (edittext text content changes, triggers the corresponding callback function) * ontextchanged () edittext When text content changes triggers * beforetextchanged () edittext text content to trigger before change * AftertextchAnged () edittext text content changes trigger * For this item, empty edittext should trigger */Addtextchangedlistener after edittext text content changes (new Textwatcher () {@ Override public void beforetextchanged (charsequence s, int start, int count, int in) {} @Override public void Ontextch Anged (charsequence s, int start, int before, int count) {} @Override public void aftertextchanged (Editable s) {//text changes after setting
Empty the position of the picture setimage ();
}
});
At the beginning, you should also set the location of the empty Picture setimage (); /** * Set the picture position method * When length () is greater than 0, that is, the edittext inside the text content, the picture is blue * when length () is less than 0, that is, edittext inside the text content, the picture is gray * setcompounddrawab
Leswithintrinsicbounds () Four parameters represent left upper right/private void setimage () {if (length () >0) {
Setcompounddrawableswithintrinsicbounds (Null,null,imageblue,null);
}else{setcompounddrawableswithintrinsicbounds (null, NULL, Imagegray, NULL);} public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {//Match finger left EditText case Motionevent.action_
Up://get the x y coordinate int x = (int) event.getrawx () when the finger leaves the edittext;
int y = (int) Event.getrawy (); Create a rectangular Rect Rect =New Rect ();
Let the width of the rectangle equal to the width of the edittext, so that the rectangle is higher than the EditText high getglobalvisiblerect (rect);
Shorten the rectangle to the right 50 width rect.left = rect.right-50;
If the x and Y coordinates are in the rectangle, you click the xx picture on the right and empty the input box if (Rect.contains (x,y)) {SetText ("");}
Break
Default:break;
Return Super.ontouchevent (event); }
}

Custom Myedittext This class is actually the same as the EditText control we dragged inside the layout layout file, Because Myedittext inherits from the parent class EditText. It's just that we should include the full package name when we call layout the layout file, as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_main "android:layout_width="
Match_parent "android:layout_height=" match_parent "android:paddingbottom=" "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin "Android:paddingtop=" @dimen/activity_vertical_margin "tools:context=" myself.myapplication.MainActivity "> < Com.liheng.MyEditText android:layout_width= "500DP" android:layout_height= "50DP" android:inputtype= "Textpersonname
"Android:text=" Name "android:ems=" android:layout_alignparenttop= "true" android:layout_alignparentleft= "true" Android:layout_alignparentstart= "true" android:layout_marginleft= "45DP" android:layout_marginstart= "45DP" Android : layout_margintop= "49DP" android:id= "@+id/edittext" android:paddingleft= "10DP" android:paddingright= "10dP "android:background=" @drawable/select_edittext "/> <com.liheng.myedittext android:layout_width=" 500DP " android:layout_height= "50DP" android:inputtype= "Textpersonname" android:text= "Name" android:ems= "Ten" Android:
layout_below= "@+id/edittext" android:layout_alignleft= "@+id/edittext" android:layout_alignstart= "@+id/editText" android:layout_margintop= "89DP" android:id= "@+id/edittext2" android:paddingleft= "10DP" android:background= "@ Drawable/select_edittext "/> </RelativeLayout>

Attach the actual effect chart:

Careful observation shows that the empty icon for the first input box leans to the left, because I added the android:paddingright= "10DP" line of code to the first input box's XML layout file. So, for the location of the empty icon, We can make specific adjustments according to the size of the mobile phone resolution.

Icon Footage:


The above is a small set to introduce the Android Studio EditText click icon to clear the text content, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.