How can I disable all views inside the layout?

后端 未结 23 1293
一个人的身影
一个人的身影 2020-11-28 08:46

For example I have:



        
23条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 09:46

    tutu's answer is on the right track, but his recursion is a little awkward. I think this is cleaner:

    private static void setViewAndChildrenEnabled(View view, boolean enabled) {
        view.setEnabled(enabled);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                setViewAndChildrenEnabled(child, enabled);
            }
        }
    }
    

提交回复
热议问题