Loonandroid Auto-detect input Box---author:rose && lvyerose@163.com

Source: Internet
Author: User

Loonandroid framework, at the same time give us a set of automatic detection of input rules of the tool, it is very convenient to use, the following describes how this stuff (note that the description is based on the project has integrated the Loonandroid framework, if you do not integrate the framework, Please follow the relevant instructions to integrate the framework before you can use it):

1.Validator Validator Class ① instantiation: Validator Validator = new Validator (this); ② set Listener callback: Validator.setvalidationlistener (...) Where the Listener interface is: Validationlistener, he has 2 ways to override the implementation: Onvalidationsucceeded ()---callback when the validator validation succeeds Onval Idationfailed (View Failedview, rule<?> failedrule)---The callback when the validator validation fails (the failure condition is that the input form does not meet the rules specified by the current validator) ③ The boot validator is validated---generally written in OK button in Validator.validate (); 2.Validation rules We understand the validator class, the following describes how to develop our own validation rules, for example, the user name input box requires the input of several characters, the letter + number composition, password input box requirements greater than 6 characters, registration requires password two times the same, and so on these rules, the framework provides us with a unified management (Uniform use of annotations for validation rule setting) ① @Regex () regular expression annotations, where parameters are: message = "First Error", pattern = "[a-za-z0-9_]{6,15}", Trim = true, order = 1 message is a validation prompt, pattern is a regular expression, trim to remove the string two-headed space, order is a priority prompt, the lower the number of priority ② @TextRule () text rule, where the parameters are: Mes Sage = "Password length 4 to 16 bits", MinLength = 4, Trim = true, MaxLength =, order = 2③ @Password () password input rules, where parameters are: message = "Password length error", MinLength = 6, MaxLength =, order = 1④ @ConfirmPassword () Confirm password input rules, where parameters are: message = "Two times password input inconsistency", Ord ER = 2 Note ③ and ④ correspond to use 3.Validation error message Description     We have validation rules and validators, and validators use our custom validation rules to verify that when the validation results are successful, we can onvalidationsucceeded the successful callback method () After the success of the logic to write, such as to get the input box string to login, registration     And when we failed to verify, how to prompt it? For example, the input does not conform to our established must have the letter + number combination of rules, entered the password 2 times inconsistent, and so on, how can we inform the user that he is currently input does not conform to which rule? Let's introduce another callback method: onvalidationfailed (View view, rule<?> Rule);    First look at his parameters, view view, if you have a certain foundation, Then you can go to see the source code to be familiar with, if not, then I can explain to you, in fact, change the view is the current user input does not conform to the rules of the input box, that is, the error of the input box, all of us can be in the verification failed callback in the following judgment:        if (view instanceof EditText) {...  }   determines the current error callback because the input box content does not conform to the rules of the     and the second parameter  rule< > Rule, an abstract class in which a private member property is: String Mfailuremessage, and the property is assigned at the bottom of the error, and its value is the value of the message parameter on the current error annotation, which can be learned from this The message parameter on the previous annotation functions. The rule class provides a method             public String getfailuremessage () {        &NBS P           return mfailuremessage;               }    So we can pass   rule.getfailuremessage () to the error parameterTo extract it   4.Complete use of the process: ①, define the control when the following annotations: ② instantiate the validator, and set the listener (set the listener parameter is this is because the current class has implemented the Listener interface) ③ Click on the event to start validation ④ verification success, can be related to the logic ⑤ validation failure in the validation failure method you can make a simple toast prompt, or you can set the Handler_textstyle style in code, and then prompt for the current view call SetError () method 6.The final effect is as shown (please set the Handler_textstyle style for the specific beautification)

Loonandroid Auto-detect input Box---author:rose && [email protected]

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.