Android-automatically disable all child controls in the layout

Source: Internet
Author: User

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 ");
}
}
}

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.