开发者

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

开发者 https://www.devze.com 2023-03-08 06:56 出处:网络
For android development, let\'s say I have declared several ImageButtons in XML In my .java file, how can I do something like:

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 w开发者_运维技巧ith 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
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消