zoukankan      html  css  js  c++  java
  • Android技术——在Android中的随意视图中找控件

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

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

  • 相关阅读:
    java内置数据类型
    docker安装及配置
    redis持久化
    golang linux安装
    TCP/IP协议
    php高并发,大流量
    C语言阐述进程和线程的区别
    python 消息队列-rabbitMQ 和 redis介绍使用
    python 新手题目-文件读写-关键字判断
    python IO模式(多路复用和异步IO深入理解)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6745218.html
Copyright © 2011-2022 走看看