android - 如何遍历 Android 中的所有 Activity 组件( View )

标签 android

我需要获取所有 Activity 组件的数组,然后我需要遍历此数组并选择,例如,仅选择按钮。 这该怎么做 ?

最佳答案

这可能对你有帮助...

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);
        }
    }
}

关于android - 如何遍历 Android 中的所有 Activity 组件( View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21202699/

相关文章:

android - NoClassDefFoundError,jar 存在

android, NumberPicker 越界

android - Hello Gridview Android 教程 - 运行时黑屏

android - Android中保存文件的正确路径

android - 是否可以在 youtube 播放器 API 上播放非 youtube 视频?

java - 调用 initLoader() 后 Loader 不启动?

android - 是否可以在 android Room Query 中使用 StringFormat 或 Constant Variable

android - iBeacon的技术能力

机器人 : media player - pause called in state 8

android - 在 Android 4.2 快速设置面板中添加项目