簡體   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