[英]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.