I haven't written a blog for a long time. Today I want to share with you a small function that is very practical.
Use Case: Disable all the child controls in the layout so that the interface can only be viewed but not edited. It is very troublesome to set one control, so I thought of a good method. You can expand more controls as needed, as long as you pay attention to the ViewGroup type or View type of the control.
The Code is as follows:
[Java]
/**
* Traverse the layout and disable all child Controls
*
* @ Param viewGroup
* Layout object
*/
Public static void disableSubControls (ViewGroup viewGroup ){
For (int I = 0; I <viewGroup. getChildCount (); I ++ ){
View v = viewGroup. getChildAt (I );
If (v instanceof ViewGroup ){
If (v instanceof Spinner ){
Spinner spinner = (Spinner) v;
Spinner. setClickable (false );
Spinner. setEnabled (false );
Log. I (TAG, "A Spinner is unabled ");
} Else if (v instanceof ListView ){
(ListView) v). setClickable (false );
(ListView) v). setEnabled (false );
Log. I (TAG, "A ListView is unabled ");
} Else {
DisableSubControls (ViewGroup) v );
}
} Else if (v instanceof EditText ){
(EditText) v). setEnabled (false );
(EditText) v). setClickable (false );
Log. I (TAG, "A EditText is unabled ");
} Else if (v instanceof Button ){
(Button) v). setEnabled (false );
Log. I (TAG, "A Button is unabled ");
}
}
}