2011-11-28 73 views
5

鉴于Android中的新屏幕,我想遍历所有的视图组和视图,以便发现所有的按钮,文本字段,微调等......这可能吗?遍历视图组

+1

入住此答案由罗曼盖伊: http://stackoverflow.com/questions/2597230/loop-through-all-subviews-of-an-android-view –

回答

7

我得到的观看次数,然后使用它作为一个 计数器调用getChildAt(INT指数)

4

这个问题可能已经长回答,但我写了这个递归函数来设置,我觉得任何按钮onClickListeners在我的布局,但它可以改变用途:

private void recurseViews(ViewGroup v) { 
    View a; 
    boolean isgrp = false; 
    for(int i = 0; i < v.getChildCount(); i++) { //attach listener to all buttons 
     a = v.getChildAt(i); 
     if(a instanceof ViewGroup) setcl((ViewGroup) a); 
     else if(a != null) { 
      //do stuff with View a 
     } 
    } 
    return; 
} 

编辑:铸造一个查看的ViewGroup作为曾经我以前认为不会引发异常,所以代码要短得多,现在