Android Check Input

Source: Internet
Author: User

In the development process, you will often encounter such requirements: There are many input controls, and so on all the input is valid, the button will automatically become enabled state, in order to continue the next operation.

The following is a solution implemented with the observer pattern.

Button code:

 Public classKwbuttonextendsButtonImplementsObserver {PrivateLinkedhashset<verifiable> mverifiers =NewLinkedhashset<verifiable>();  PublicKwbutton (Context context) {Super(context);    Initview (); }     PublicKwbutton (Context context, AttributeSet attrs) {Super(context, attrs);    Initview (); }     PublicKwbutton (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle);    Initview (); }    Private voidInitview () {//Initialize View    }    /*** Add Observer * *@paramverifier*/     Public voidObserver (Verifiable verifier) {if( This. IsEnabled ()) {             This. setenabled (false); }        //Checksum: If the current verifier belongs to view but is not displayed, do not listen        if((verifierinstanceofView)&& (View) verifier. getvisibility ()! =view.visible) {Update (NULL,NULL); return; }        if(Verifier! =NULL&&!mverifiers.contains (verifier))            {Mverifiers.add (verifier); //where the essence isVerifier.addobserver ( This); } Update (NULL,NULL); } @Override Public voidSetEnabled (Booleanenabled) {        Super. setenabled (enabled); }    /*** Remove Observation * *@paramverifier*/     Public voidremoveobserver (Verifiable verifier) {if(Verifier! =NULL) {mverifiers.remove (verifier);  This. Update (NULL,NULL); }    }    /*** Clear the viewer*/     Public voidClearobserver () {if(!Listutil.isempty (mverifiers))            {mverifiers.clear (); Mautoperformclick=false;  This. Update (NULL,NULL); }} @Override Public voidUpdate (Observable Observable, Object data) {if(Mautoperformclick) {if(!Listutil.isempty (mverifiers)) {                if(Isverify ()) { This. postdelayed (NewRunnable () {@Override Public voidrun () {PerformClick ();                }}, Perform_delay_time); }            }        } Else {             for(verifiable verifier:mverifiers) {if(Verifier.isblank ()) {Kwbutton. This. setenabled (false); return; }} Kwbutton. This. setenabled (true); }    }    /*** has passed validation * *@return     */    Private Booleanisverify () { for(verifiable verifier:mverifiers) {if(!verifier.verify ()) {                return false; }        }        return true; }}

Check interface:

/** * Check interface *   */  Public Interface verifiable {    /**     * Check     *      @return* *         boolean  verify ();
boolean IsBlank (); void addobserver (Observer obj);}

Input CONTROLS:

/*** Input Controls*/ Public classNeedcheckinputextendsEditTextImplementsVerifiable {PrivateContext Mcontext; /*** Check*/    PrivateObserver Mverifyobserver =NULL;  PublicNeedcheckinput (Context context) {Super(context); Mcontext=context;    Init (); }     PublicNeedcheckinput (Context context, AttributeSet attrs) {Super(context, attrs); Mcontext=context;    Init (); } @SuppressLint ("Inflateparams")    Private voidinit () {//Initialize View Code    }    /*//Notify the Observer that the control state has changed, update once, check once if (mverifyobserver! = null) {m                Verifyobserver.update (null, NULL); }     */@Override Public BooleanVerify () {//Add a check rule true: Verify pass false: Checksum does not pass        return! Textutils.isempty ( This. GetText (). toString ()); }

@Override
public Boolean IsBlank () {
Return ....;
} @Override Public voidaddobserver (Observer obj) {mverifyobserver=obj; }}

Example code:

Kwbutton button  new New New needcheckinput (context); Button.addobserver (INPUT1); Button.addobserver (INPUT2);

This makes it possible to check the input.

Android Check Input

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.