How can I disable all views inside the layout?

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

For example I have:



        
23条回答
  •  死守一世寂寞
    2020-11-28 09:41

    I improved the tütü response to properly disable EditText and RadioButton componentes. Besides, I'm sharing a way that I found to change the view visibility and add transparency in the disabled views.

    private static void disableEnableControls(ViewGroup view, boolean enable){
        for (int i = 0; i < view.getChildCount(); i++) {
            View child = view.getChildAt(i);
            child.setEnabled(enable);
            if (child instanceof ViewGroup){
                disableEnableControls((ViewGroup)child, enable);
            }
            else if (child instanceof EditText) {
                EditText editText = (EditText) child;
                editText.setEnabled(enable);
                editText.setFocusable(enable);
                editText.setFocusableInTouchMode(enable);
            }
            else if (child instanceof RadioButton) {
                RadioButton radioButton = (RadioButton) child;
                radioButton.setEnabled(enable);
                radioButton.setFocusable(enable);
                radioButton.setFocusableInTouchMode(enable);
            }
        }
    }
    
    public static void setLayoutEnabled(ViewGroup view, boolean enable) {
        disableEnableControls(view, enable);
        view.setEnabled(enable);
        view.setAlpha(enable? 1f: 0.3f);
    }
    
    public static void setLayoutEnabled(ViewGroup view, boolean enable, boolean visibility) {
        disableEnableControls(view, enable);
        view.setEnabled(enable);
        view.setAlpha(enable? 1f: 0.3f);
        view.setVisibility(visibility? View.VISIBLE: View.GONE);
    }
    

提交回复
热议问题