質問

enter image description here

I want to disable all the field inside the scroll view shown in the picture. I tried using the code below but the code only disables the direct child of linear layout and doesn't disable child for the nested linear layout. How can I disable all child including the children of nested layouts?

LinearLayout myLayout
    = (LinearLayout)v.findViewById(R.id.addEditSection1);

for (int i = 0; i < myLayout.getChildCount();  i++) {
    View view = myLayout.getChildAt(i);
    view.setEnabled(false);
}
役に立ちましたか?

解決

Try this recursive function:

public void disableAllViews(View v){
    v.setEnabled(false);
    if(v instanceof ViewGroup){

    for (int i = 0; i < ((ViewGroup)v).getChildCount();  i++) {
        View view = ((ViewGroup)v).getChildAt(i);
        disableAllViews(view);
    }
    }
}

And call it as

LinearLayout myLayout
    = (LinearLayout)v.findViewById(R.id.addEditSection1);
disableAllViews(myLayout);

他のヒント

Quiet trivial: You got to check recursive. E.g.:

protected void disableViewElements(ViewGroup container) {
   for (int i = 0; i < container.getChildCount();  i++) {
     if(container.getChildAt(i) instanceof ViewGroup ) {
         disableViewElements((ViewGroup) container.getChildAt(i));
     }
     else {
       View view = container.getChildAt(i);
       view.setEnabled(false);
     }
   }

}

This is really easy with droidQuery:

$.with(myLayout).selectAll().disable();

I am using this in my project

public void setAllViewsEnabled(View view, boolean enabled) {
    if (view instanceof ViewGroup)
        for (int i = 0; i < ((ViewGroup)view).getChildCount();  i++)
            setAllViewsEnabled(((ViewGroup)view).getChildAt(i), enabled);
    view.setEnabled(enabled);
}

You can check to execute the last line weather the current view is a TextBox. You can also decide to enable it after using this function.

Remember that if you disable a nested ViewGroup vg1 and then you disable the containing ViewGroup vg0, re-enabling vg0 will result in enabing also vg1. To prevent this you have to save a boolean for each ViewGroup and to change the method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top