繁体   English   中英

遍历 XML 中声明的每个小部件,并在 java 和 android 开发中执行操作

[英]Iterate through each widget declared in XML and perform an action in java and android development

对于 android 开发,假设我在 XML 中声明了几个 ImageButtons

在 my.java 文件中,我该怎么做:

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

只需遍历视图容器的所有后代并为满足某些条件的每个视图执行操作:

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
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM