3
我需要獲取所有活動組件的數組,然後我需要遍歷這個數組並選擇,例如,只有按鈕。 如何做到這一點?如何遍歷Android中的所有活動組件(視圖)
我需要獲取所有活動組件的數組,然後我需要遍歷這個數組並選擇,例如,只有按鈕。 如何做到這一點?如何遍歷Android中的所有活動組件(視圖)
這可能會幫助你...
public ArrayList<Button> getButtons() {
ArrayList<Button> buttons = new ArrayList<Button>();
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
findButtons(viewGroup, buttons);
return buttons;
}
private static void findButtons(ViewGroup viewGroup,ArrayList<Button> buttons) {
for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
findButtons((ViewGroup) child, buttons);
} else if (child instanceof Button) {
buttons.add((Button) child);
}
}
}
此信息可能是也有幫助:http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity – Doc