How can I disable all views inside the layout?

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

For example I have:



        
相关标签:
23条回答
  • 2020-11-28 09:29

    Let's change tütü's code

    private void disableEnableControls(boolean enable, ViewGroup vg){
    for (int i = 0; i < vg.getChildCount(); i++){
       View child = vg.getChildAt(i);
       if (child instanceof ViewGroup){ 
          disableEnableControls(enable, (ViewGroup)child);
       } else {
         child.setEnabled(enable);
       }
     }
    }
    

    I think, there is no point in just making viewgroup disable. If you want to do it, there is another way I have used for exactly the same purpose. Create view as a sibling of your groupview :

    <View
        android:visibility="gone"
        android:id="@+id/reservation_second_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:background="#66ffffff"
        android:clickable="false" />
    

    and at run-time, make it visible. Note: your groupview's parent layout should be either relative or frame layout. Hope this will help.

    0 讨论(0)
  • 2020-11-28 09:29

    I like to have a control over the root view so I added the includeSelf flag to deepForEach

    fun ViewGroup.deepForEach(includeSelf: Boolean = true, function: View.() -> Unit) {
        if (includeSelf) function()
        forEach {
            it.apply {
                function()
                (this as? ViewGroup)?.apply { deepForEach(includeSelf, function) }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:29
    You can have whichever children that you want to have the same state as the parents include android:duplicateParentState="true".  Then if you disable the parent, whatever children you have that set on will follow suit.  
    

    This will allow you to dynamically control all states at once from the parent view.

    <LinearLayout
        android:id="@+id/some_parent_something"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         <Button 
            android:id="@+id/backbutton"
            android:text="Back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:duplicateParentState="true"/>
        <LinearLayout
            android:id="@+id/my_layout"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:duplicateParentState="true">
            <TextView
                android:id="@+id/my_text_view"
                android:text="First Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:duplicateParentState="true" />
            <EditText
                android:id="@+id/my_edit_view"
                android:width="100px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:duplicateParentState="true" /> 
            <View .../>
            <View .../>
            ...
               <View .../>
        </LinearLayout>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-28 09:30

    If you want to disable a set of, or say a particular kind of view.Let's say you want to disable a fixed number of buttons with some particular text of or no text then you can use array of that type and loop through the array elements while disabling the buttons using setEnabled(false) property You can do it on a function call like this:

    public void disable(){
            for(int i=0;i<9;i++){
                    if(bt[i].getText().equals("")){//Button Text condition
                        bt[i].setEnabled(false);
                }
            }
    }
    
    0 讨论(0)
  • 2020-11-28 09:31

    Another way is to call setEnabled() on each child (for example if you want to do some extra check on child before disabling)

    LinearLayout layout = (LinearLayout) findViewById(R.id.my_layout);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        child.setEnabled(false);
    }
    
    0 讨论(0)
  • 2020-11-28 09:32

    to disable a view, you must call the method setEnabled with false for argument. ex:

    Button btn = ...
    btn.setEnabled(false);
    
    0 讨论(0)
提交回复
热议问题