博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android技术——在Android中的随意视图中找控件
阅读量:4696 次
发布时间:2019-06-09

本文共 651 字,大约阅读时间需要 2 分钟。

1、在非常多情况下,我们可能不知道控件的id,可是我们却希望在包括这个控件的视图中找到它,能够採用例如以下做法:

例:在Activity的根视图中找出当中全部的Button控件

    private void findButton(ViewGroup group, List<Button> result)

    {
        if (group != null)
        {
            for (int i = 0, j = group.getChildCount(); i < j; i++)
            {
                View child = group.getChildAt(i);
                if (child instanceof Button)
                {
                    result.add((Button) child);
                } else if (child instanceof ViewGroup)
                {
                    findButton((ViewGroup) child, result);
                }
            }
        }
    }

在Activity中调用:

        List<Button> result = new ArrayList<Button>();

        this.findButton((ViewGroup) this.getWindow().getDecorView(), result);

这种方法事实上就是递归地从根视图開始查找整个控件树,终于找到符合要求的控件,稍加改写就能够满足各种找控件的需求。

转载于:https://www.cnblogs.com/lxjshuju/p/6823347.html

你可能感兴趣的文章
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>
初级ant的学习
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>
C#和JAVA 访问修饰符
查看>>