獲取Android Activity上所有的控件

jopen 10年前發布 | 45K 次閱讀 Android Android開發 移動開發

在android開發中,在表單提交的場景下想獲取activity上所有的控件

/**

  • @note 獲取該activity所有view

  • */

public List<View> getAllChildViews() {

View view = this.getWindow().getDecorView();

return getAllChildViews(view);

}

private List<View> getAllChildViews(View view) {

List<View> allchildren = new ArrayList<View>();

if (view instanceof ViewGroup) {

ViewGroup vp = (ViewGroup) view;

for (int i = 0; i < vp.getChildCount(); i++) {

View viewchild = vp.getChildAt(i);

allchildren.add(viewchild);

allchildren.addAll(getAllChildViews(viewchild));

}

}

return allchildren;

}</pre></span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!