Android 在Activity里查找某个子View是否存在

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010095372/article/details/77981482

判断在Activity里是否存在,我想到了遍历一遍Activity的子View,然后对Tag,对上了就是有,没对上就是没有。要递归着找。

 //是否存在这个子viewA,返回true就是有,false就是没有
    private Boolean getChildA(View view) {
        Boolean a = false;
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                //里面的1000即为需要找View的tag
                if(viewchild.getTag() != null && String.valueOf(viewchild.getTag()).equals("1000")) {
                    Log.i("已存在", String.valueOf(viewchild.getTag()));
                    return true;
                }
                a = a || getChildA(viewchild);
            }
        }
        return a;
    }

要这样用,就酱

//swin即为activity
getChildA(swin.getWindow().getDecorView())

猜你喜欢

转载自blog.csdn.net/u010095372/article/details/77981482