android 遍历所有控件

private void getButtons(ViewGroup viewGroup) {
  if (viewGroup == null) {
   return;
  }
  int count = viewGroup.getChildCount();
  for (int i = 0; i < count; i++) {
   View view = viewGroup.getChildAt(i);
   if (view instanceof Button) { // 若是Button记录下
    Button newDtv = (Button) view;
   } else if (view instanceof ViewGroup) {
    // 若是布局控件(LinearLayout或RelativeLayout),继续查询子View
    this.getButtons((ViewGroup) view);
   }
  }
 }

你可能感兴趣的:(Android)