通过XML声明的每个插件迭代,并在Java和Android开发执行的操作 [英] Iterate through each widget declared in XML and perform an action in java and android development

查看:123
本文介绍了通过XML声明的每个插件迭代,并在Java和Android开发执行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关android开发,让我们说我已经宣布XML几个ImageButtons

在我的java文件,我怎么可以这样做:

 的foreach(ImageButton的带属性=XYZ){做到这一点}


解决方案

只需通过视图容器的所有后代进行迭代,并为每一个满足一定的条件视图执行操作:

 私人无效iterateImages(ViewGroup中组){
    的for(int i = 0,N = group.getChildCount(); I< N ++我){
        查看孩子= group.getChildAt(I)
        如果(孩子的instanceof的ViewGroup){
            iterateImages((ViewGroup中)的孩子);
        }否则如果(孩子的instanceof ImageView的){
            ImageView的图像=(ImageView的)子女;
            如果(/ *属性XYZ* /){
                // 做这个
            }
        }
    }
}

For android development, let's say I have declared several ImageButtons in XML

In my .java file, how can I do something like:

foreach (ImageButton with attribute="xyz") {do this}

解决方案

Just iterate through all descendants of a view container and perform an action for every view that satisfies some conditions:

private void iterateImages(ViewGroup group) {
    for (int i = 0, n = group.getChildCount(); i < n; ++i) {
        View child = group.getChildAt(i);
        if (child instanceof ViewGroup) {
            iterateImages((ViewGroup)child);
        } else if (child instanceof ImageView) {
            ImageView image = (ImageView)child;
            if ( /* attribute"xyz" */ ) {
                // do this
            }
        }
    }
}

这篇关于通过XML声明的每个插件迭代,并在Java和Android开发执行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆