Explain the Android control state dependency framework in detail
In the development of production Android client software (enterprise application), there may be multiple inputs (EditText) and multiple operations (motionevent and keyevent) on the interface, and the operations depend on the input state. As shown in the following figure:
In the setting diagram
The input box has three states:
The operation can only be executed when the dependent input data is verified successfully.
If you judge the input box status in the activity, you actually need to call (3 input boxes) * (3 states) * (3 buttons) = 27 If judgments. The maintenance of the status will make the maintainability of the whole program extremely poor, and the maintenance status increases exponentially with the increase of input and operation.
Through the abstraction of this scenario, the Android control state dependency framework is implemented, and its use method is as follows:
usage method:
1. The layout file references watchedittext and watchbutton
Since the control ID in the library module is not a constant (refer to the reason why butterknife supports R2 for library module), the tag method is adopted here.
2. Declare dependencies through annotations in an activity
VIEWNAME defines the control name, and dependency in viewdependency specifies the control tag it depends on.
3. Directly execute onclick and oneditoraction (modify status)
It can be seen that the status of each input control is not judged by if.
Oneditoraction simulates calling the enter of the software for verification. Here, you need to pay attention to modifying the status of the eidttext through editquery1. Complete().
Implementation principle
The whole framework is divided into three packages: annotation, state and view.
1. Define the VIEWNAME and viewdependency annotations in the annotation, which are used for watchedittext and watchbutton respectively. VIEWNAME specifies the name of the watchedittext control in the business, and viewdependency specifies the watchedittext control that the watchbutton depends on;
2. In state, enter, verify and complete are defined through the state mode. The base class is the abstract class operator, and the method operator is defined;
3. Watchedittext and watchbutton define the dependencies of the control. Watchedittext implements the viewstate interface, which contains three state conversion methods.
Above, the blog Park does not support markdown very well and cannot add comments (/ * * /). To view the source code, please move to GitHub address: https://github.com/yhthu/AndroidViewDependency.git
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.