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