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

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

  • 相关阅读:
    Codeforces Round #370 (Div. 2)
    Codeforces Round #425 (Div. 2)
    变量调节器
    Smarty基础
    流程
    iframe 内联框架
    权限:改变权限
    权限:查找
    html 框架
    Jcrop+uploadify+php实现上传头像预览裁剪
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6745218.html
Copyright © 2011-2022 走看看