Mainactivity is as follows:
Package cc.textview5;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
Import android.app.Activity;
/** * Demo Description: * Custom control implementation with the removal of edittext * * Learning materials: * http://blog.csdn.net/xiaanming/article/details/11066685 * * Thank you very very much/public class Mainactivity extends activity {private Cleanableedittext Musernamecleanableed
Ittext;
Private Cleanableedittext Mpasswordcleanableedittext;
Private Button Mloginbutton;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Init ();
private void Init () {musernamecleanableedittext= (Cleanableedittext) Findviewbyid (R.id.usernameedittext);
mpasswordcleanableedittext= (Cleanableedittext) Findviewbyid (R.id.passwordedittext); mloginbutton= (Button) Findviewbyid (r.id.Loginbutton);
Mloginbutton.setonclicklistener (New Onclicklistenerimpl ()); Private class Onclicklistenerimpl implements Onclicklistener {@Override public void OnClick (view view) {if (Textutils.isempty (Musernamecleanableedittext.gettext ())) {Musernamecleanableedittext.setshakeanima
tion ();
Toast.maketext (Mainactivity.this, "Please enter user name", Toast.length_short). Show (); } if (Textutils.isempty (Mpasswordcleanableedittext.gettext ())) {Mpasswordcleanableedittext.setshakeanim
Ation ();
Toast.maketext (Mainactivity.this, "Please enter password", Toast.length_short). Show ();
}
}
}
}
The
Cleanableedittext is as follows:
Package cc.textview5;
Import Android.content.Context;
Import android.graphics.drawable.Drawable;
Import android.text.Editable;
Import Android.text.TextWatcher;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import android.view.animation.Animation;
Import Android.view.animation.CycleInterpolator;
Import android.view.animation.TranslateAnimation;
Import Android.widget.EditText; /** * To determine whether to show the right clean icon/public class Cleanableedittext extends EditText {private drawable Mrig when focus changes and input content changes
htdrawable;
Private Boolean Ishasfocus;
Public Cleanableedittext {Super (context);
Init ();
Public Cleanableedittext (context, AttributeSet attrs) {Super (context, attrs);
Init ();
Public Cleanableedittext (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Init (); private void Init () {//GETCOMPOUNDDRAWABLES://returns drawables for the left, top, right, and bottom borders.
drawable [] drawables=this.getcompounddrawables ();
Get right position drawable//that we set in the layout file android:drawableright mrightdrawable=drawables[2];
Set the monitoring This.setonfocuschangelistener for focus changes (new Focuschangelistenerimpl ());
Set the EditText text-changing listening this.addtextchangedlistener (new Textwatcherimpl ());
Let the right clean icon not be visible setcleardrawablevisible (false) when initialized; /** * When the finger is raised in the area of the clean icon * We treat this as a purge operation * getwidth (): Get the width of the control * EVENT.GETX (): The coordinates of the lift (change coordinates are relative to the control In itself) * Gettotalpaddingright (): Clean icon to the left edge of the control to the right edge of the distance * getpaddingright (): Clean icon to the right edge to the right edge of the control * so: * get Width ()-Gettotalpaddingright () indicates: * The area to the left of the clean icon to the left of the control * getwidth ()-Getpaddingright () is: * The left side of the control to the clean icon right The area of the edge * so the area between the two is just the area of the clean icon * * @Override public boolean ontouchevent (Motionevent event) {switch ( Event.getaction ()) {Case MotiOnEvent.ACTION_UP:boolean Isclean = (Event.getx () > (GetWidth ()-gettotalpaddingright ()) &&
(Event.getx () < (getwidth ()-getpaddingright ());
if (Isclean) {SetText ("");
} break;
Default:break;
Return Super.ontouchevent (event); Private class Focuschangelistenerimpl implements onfocuschangelistener{@Override public void Onfocusch
Ange (View V, boolean hasfocus) {ishasfocus=hasfocus;
if (Ishasfocus) {Boolean isvisible=gettext (). toString (). Length () >=1;
Setcleardrawablevisible (isVisible);
else {setcleardrawablevisible (false); Verify that the right clean icon is displayed when the input is finished private class Textwatcherimpl implements textwatcher{@Overrid
e public void aftertextchanged (Editable s) {Boolean isvisible=gettext (). toString (). Length () >=1; Setcleardrawablevisible (isVisible);
@Override public void beforetextchanged (charsequence s, int start, int count,int after) {
@Override public void ontextchanged (charsequence s, int start, int before,int count) {} //Hide or show the right clean icon protected void setcleardrawablevisible (Boolean isVisible) {drawable rightdrawabl
E
if (isVisible) {rightdrawable = mrightdrawable;
else {rightdrawable = null; ///Use code to set the bottom icon Setcompounddrawables (Getcompounddrawables () () [0],getcompounddrawable
S () [1], Rightdrawable,getcompounddrawables () [3]);
//Displays an animation to prompt the user to enter public void Setshakeanimation () {this.setanimation (Shakeanimation (5)); //cycletimes Animation Repeat number public Animation shakeanimation (int cycletimes) {Animation translateanimation = new Tr
Anslateanimation (0, 10, 0, 10); Translateanimation.setinterpolator (New Cycleinterpolator (CycleTimes));
Translateanimation.setduration (1000);
return translateanimation;
}
}
The
Main.xml is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi" ngleft= "20dip" android:paddingright= "20dip" > <cc.textview5.cleanableedittext android:id= "@+id/usern Ameedittext "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:hint=" username "Android:layout_margintop=" 30dip "android:drawableleft=" @drawable/icon_user "android:drawableright=" @drawa Ble/clean_selector "/> <cc.textview5.cleanableedittext android:id=" @+id/passwordedittext "an Droid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:hint= "password" ANDROID:PASSW Ord= "true" android:layout_margintop= "100dip" android:drawableleft= "@drawable/account_icon" android:drawable right= "@drawable/clean_selector"/>
<button android:id= "@+id/loginbutton" android:layout_width= "Fill_parent" Android:layout_heigh
t= "Wrap_content" android:text= "Login" android:layout_margintop= "155dip"/> </RelativeLayout>
Above is the Android with delete button EditText data collation, follow-up to continue to supplement the relevant information, thank you for your support to this site!