Nonsense Hugh, first on.
Android custom components, the methods you need to rewrite are: two-parameter construction methods (layout loading must), OnDraw (), Ontouchevent (), onfocuschanged () and so on, many of the components also need to customize AttributeSet. The implementation of this component is achieved primarily through the use of ontouchevent, onfocuschanged, and Textwatcher, as well as a custom animation.
The main idea is to control the explicit concealment of the button through Getconpounddrawable () and setcompounddrawable () in the onfocuschanged and OnTextChanged methods, and then to judge the coordinates by the Ontouch event. Simulate click events to control clear text, define an animation method, click Submit Call.
On code: Public class Clearedittext extends EditText implements Onfocuschangelistener,textwatch
Privatedrawable mrightdrawable; PublicClearedittext (Context context) { This(Context,NULL); } PublicClearedittext (Context context, AttributeSet attrs) { This(context, attrs,android. R.attr.edittextstyle); //TODO auto-generated Constructor stub } PublicClearedittext (Context context,attributeset attrs,intDefstyle) { Super(context, attrs, Defstyle); Initclearedittext (); } Private voidInitclearedittext () {mrightdrawable= Getcompounddrawables () [2]; if(Mrightdrawable = =NULL) {mrightdrawable=getresources (). getdrawable (R.drawable.clear); } setrightdrawablevisible (false); Setonfocuschangelistener ( This); Addtextchangedlistener ( This); } Private voidSetrightdrawablevisible (Booleanb) {//TODO auto-generated Method StubLOG.D ("setvisible", "B ' s value is:" +string.valueof (b)); drawable myflagdrawable= b? Mrightdrawable:NULL; if(Myflagdrawable = =NULL) {LOG.D ("SetVisible", "Myflagdrawble is null"); } Else{myflagdrawable.setbounds (0, 0, myflagdrawable.getintrinsicwidth (), Myflagdrawable.getintrinsicheight ());//This is required or this is invisibleLOG.D ("setvisible", "myflagdrawble is not NULL"); } setcompounddrawables (Getcompounddrawables () [0], Getcompounddrawables () [1], myflagdrawable, Getcompounddrawables () [3]); } @Override Public voidOnfocuschange (View V,BooleanHasfocus) { //TODO auto-generated Method StubLOG.D ("Focus", String.valueof (Hasfocus)); if(Hasfocus = =true) { Booleanvisibility = GetText (). toString (). Length () >0?true:false; Setrightdrawablevisible (visibility); } Else{setrightdrawablevisible (hasfocus); }} @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method Stub floatx =Event.getx (); //Float y = event.gety (); Switch(Event.getaction ()) { Casemotionevent.action_up:if(X>getwidth ()-mrightdrawable.getintrinsicwidth () &&x<getwidth ()) {SetText (""); } Break; default: Break; } return Super. Ontouchevent (event); } @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { //TODO auto-generated Method Stub} @Override Public voidaftertextchanged (Editable s) {//TODO auto-generated Method Stub} @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) {LOG.D ("TextChange", "changed" ); Onfocuschange ( This,true); }; Public voidStartshake (intcounts) {Animation Animation=NewTranslateanimation (0, 10, 0, 0); Animation.setinterpolator (NewCycleinterpolator (counts));//gradient, the number of displacement animation frames is repeated according to the sine function sinusoidal
Animation.setduration (1000); Startanimation (animation); }}
Callback Onfoucuschange in ontextchanged to set the Delete button's explicit
EditText for custom Component jitter