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