Eu tenho um número de linhas no meu TableLayout e quero acessar as exibições de botão. Exemplo da seguinte forma:
<TableLayout>
<TableRow>
<Button />
<Button />
</TableRow>
<TableRow>
<Button />
<Button />
</TableRow>
</TableLayout>
A partir do layout da tabela, gostaria de percorrer os botões.
TableLayout layout=(TableLayout) findViewById(R.id.Layout);
layout.getChildCount;
O acima apenas retorna o número de visualizações tablerow.
Respostas:
3 para resposta № 1Tente assim, pode ser útil para você
TableLayout layout = (TableLayout) findViewById(R.id.Table_ID);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof TableRow) {
TableRow row = (TableRow) child;
for (int x = 0; x < row.getChildCount(); x++) {
View view = row.getChildAt(x);//Here you get Your Button View
}
}
}
0 para resposta № 2
Tente isto:
TableLayout layout=(TableLayout) findViewById(R.id.Layout);
for(int i=0;i<layout.getChildCount();i++) {
if (layout.getChildAt(i) instanceof TableRow) {
TableRow tableRow = (TableRow) layout.getChildAt(i);
for(int j=0;j<tableRow.getChildCount();j++) {
if (tableRow.getChildAt(j) instanceof Button) {
Button button = (Button) tableRow.getChildAt(j);
//your button is here
}
}
}
}