Iterate through each widget declared in XML and perform an action in java and android development
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
}
}
}
}
精彩评论