EditText for custom Component jitter

Source: Internet
Author: User

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

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.