GitHub Address: Https://github.com/yhthu/ViewDependencyUsage Scenarios:
The demo is mainly for production-oriented Android client software, there are multiple inputs and multiple operations on the interface, and the operation depends on the input state.
In the setup diagram
- Confirm that the operation depends on the status of the product code and the storage position
- Skip operation does not depend on input state
- Registering a difference operation depends on the state of the stored position and quantity
There are three states of the input box: 1) to be entered, 2) to be verified, and 3) to verify the success. The operation needs to be executed when its dependent input data validation succeeds.
If in the activity to judge the input box state, then actually need to call (3 input box) * (3 states) * (3 buttons) = 27 If judgment, the maintenance of the State will make the whole program maintainability is very poor, and with the input and operation of the increase, The state of maintenance is exponentially increasing.
How to use:
Because Jcenter is not currently uploaded, it is for reference only.
1. layout file references Watchedittext and Watchbutton
< Com.android.yhthu.viewdependency.view.WatchEditText android:id = "@+id/edit_query_1" android:layout_width = "match_parent" android:layout_height = "wrap_content" Android:tag =" EditQuery1 " android:imeoptions = "Actionnext" Android:hint = "Product Code" Android:inputtype = "number" />
< Com.android.yhthu.viewdependency.view.WatchEditText android:id = "@+id/edit_query_1" android:layout_width = "match_parent" android:layout_height = "wrap_content" Android:tag =" EditQuery1 " android:imeoptions = "Actionnext" Android:hint = "Product Code" Android:inputtype = "number" />
Because the control ID in the library module is not a constant (refer to the reason why Butterknife supports the Library module with R2), the tag is used here.
2. Use annotations to affirm dependency in activity
@ViewName ("Product Code")Privatewatchedittext editQuery1; @ViewName ("Storage Place")Privatewatchedittext EditQuery2; @ViewName (Number)Privatewatchedittext editQuery3; @ViewDependency (name= @ViewName ("Confirm"),
Dependency = {"EditQuery1", "EditQuery2"})PrivateWatchbutton buttonSearch1; @ViewDependency (name= @ViewName ("Skip")/*do not rely on input*/)PrivateWatchbutton buttonSearch2; @ViewDependency (name= @ViewName ("Register out of Stock"),
Dependency = {"EditQuery2", "EditQuery3"})PrivateWatchbutton ButtonSearch3;
ViewName defines the control name, viewdependency dependency specifies the control tag that it relies on.
3. Direct execution of OnClick and oneditoraction (modified state)
@Override public void OnClick (View v) { if ( V == ButtonSearch1) {Toast.maketext ( this , "Tune interface" else if (v == ButtonSearch2) {Toast.maketext ( this ," Skip Next ", Toast.length_short). Show (); else if (v == ButtonSearch3) {Toast.maketext ( this ," register out of stock ", Toast.length_short). Show (); }}
As you can see, the status of each input control is not judged by if.
@Override Public BooleanOneditoraction (TextView V,intActionId, KeyEvent event) { if(ActionId = = Editorinfo.ime_action_next && v = =EditQuery1&& (Query1str =Editquery1.gettext (). toString ()). IsEmpty ()) {if(Query1str.equals ("12345") ) {editquery1.complete (); return true; } } //Omit Code return false;}
Oneditoraction impersonation calls the software's enter for verification, it is important to note that the state of the Eidttext is modified by Editquery1.complete ().
Implementation principle:
The entire frame is divided into three package:annotation, state, and view.
- Define ViewName and viewdependency annotations in annotation, respectively, for Watchedittext and Watchbutton. VIEWNAME Specifies the name of the Watchedittext control in the business, viewdependency specifies the Watchedittext control that the Watchbutton relies on;
/*** control state dependent * Created by yanghao1 on 2016/12/19.*/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented Public@Interfaceviewdependency {/*** Control Name (nested annotations) * *@return */ViewName name ()default@ViewName; /*** control State dependent * *@return */string[] Dependency ()default {};}
- In the state through the * * Status mode * * Defines enter, Verify, complete, its base class is abstract class operator, define method operator;
/**-Operation Abstract interface-Created by Yanghao1 on 2016/12/15.*/ Public Abstract classOperator {//the context in which the operation corresponds protectedcontext Context; /*** Operation * *@paramoperatorname operation name *@paramviewName Control Name *@returnwhether the action can be performed*/ Public Abstract Booleanoperator (string operatorname, String viewName);}
/**-Pending input status (initial state)-Created by Yanghao1 on 2016/12/19.*/ Public classEnterextendsOperator {Private StaticEnter enter; PrivateEnter (Context context) { This. Context =context; } Public StaticEnter getinstance (context context) {if(Enter = =NULL) {Enter=NewEnter (context); } returnEnter; } @Override Public Booleanoperator (string operatorname, String viewName) {Toast.maketext (context, String.Format ("[%s] is empty, do not allow [%s]", ViewName, Operatorname), Toast.length_short). Show (); return false; }}
- Watchedittext and Watchbutton Define the dependencies of the control. The Watchedittext implements the ViewState interface, which contains three state conversion methods.
/*** Control status * Created by Yanghao1 on 2016/12/15.*/ Public InterfaceViewState {/*** Pending input status (initial state)*/ voidenter (); /*** Pending Check status (with input (not empty), but not verified, or verification not successful)*/ voidverify (); /*** has input, and verification success*/ voidComplete ();}
Android control state dependent