2011-03-11 33 views
0

我對android很新穎......任何人都可以讓我知道如何獲得視圖組的句柄?查看組實例

例如:

我在main.xml文件的線性佈局。 我能夠添加視圖的唯一方法是 使用findViewById並指定線性佈局的ID。我想開發它得到的ViewGroup中手柄和執行類似getChildCount() 等功能的通用方法...

回答

0

你可以讓自己的佈局子類,並覆蓋一些方法,如onFinishInflate()View documentation有一些關於這方面的信息,你會發現很多有關如何做到這一點的教程。

這就是我在one of my tutorials解釋得到佈局的所有Checkable視圖。

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 

    final int childCount = this.getChildCount(); 
    for (int i = 0; i < childCount; ++i) { 
     findCheckableChildren(this.getChildAt(i)); 
    } 
} 

/** 
* Add to our checkable list all the children of the view that implement the 
* interface Checkable 
*/ 
private void findCheckableChildren(View v) { 
    if (v instanceof Checkable) { 
     this.checkableViews.add((Checkable) v); 
    } 

    if (v instanceof ViewGroup) { 
     final ViewGroup vg = (ViewGroup) v; 
     final int childCount = vg.getChildCount(); 
     for (int i = 0; i < childCount; ++i) { 
      findCheckableChildren(vg.getChildAt(i)); 
     } 
    } 
} 
+0

謝謝。但我正在寫一個泛型類,它獲取所有EditText字段並將其發佈到restful資源。所以我需要開發一個通用的代碼,我可以在其他應用程序中使用。因此,我不想使用findViewById方法,在這種情況下特定於該應用程序。 – abhirami 2011-03-11 09:42:28

+0

Anwser編輯了一些代碼 – 2011-03-11 09:49:05

+0

非常感謝! – abhirami 2011-03-11 10:36:17