遍历一个布局里面所有的控件

来源:互联网 发布:在线学编程的网站 编辑:程序博客网 时间:2024/05/16 14:47
  1. private void getButtons(ViewGroup viewGroup) {  
  2.   if (viewGroup == null) {  
  3.    return;  
  4.   }  
  5.   int count = viewGroup.getChildCount();  
  6.   for (int i = 0; i < count; i++) {  
  7.    View view = viewGroup.getChildAt(i);  
  8.    if (view instanceof Button) { // 若是Button记录下  
  9.     Button newDtv = (Button) view;  
  10.    } else if (view instanceof ViewGroup) {  
  11.     // 若是布局控件(LinearLayout或RelativeLayout),继续查询子View  
  12.     this.getButtons((ViewGroup) view);  
  13.    }  
  14.   }  
  15.  }  

原创粉丝点击