2011-11-28 115 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作爲曾經我以前認爲不會引發異常,所以代碼要短得多,現在