android遍历控件的方法备忘

int count=group.getChildCount();
for(int i=0;i<count;i++){
    View view=group.getChildAt(i);
    String className=view.getClass().getSimpleName();
    if(view instanceof TableRow){

    }
    else if(view instanceof LinearLayout){

    }
    else {
        continue;
    }
}


主要有两种方法,一种是用className来判断,比如:if(className.equals("Button"));

另一种是用instanceof来判断,网上查了不少地方,发现都是这种写法:if(view instanceof ViewGroup),经本人测试,发现并不好用,可能是android sdk版本的问题,我的是21,如果要用instanceof最好是像上面的代码那样写:if(view instanceof TableRow),即写具体的容器类名就可以了。

发布了134 篇原创文章 · 获赞 26 · 访问量 77万+

猜你喜欢

转载自blog.csdn.net/yw1688/article/details/54314006