Joh*_*son 7 android button tablelayout
在3x3格式中TableLayout有9 Buttons个.如何使用TableLayout(不是按钮ID)的id以编程方式访问这些按钮上的文本?
use*_*305 19
使用类似的东西,
TableLayout tblLayout = (TableLayout)findViewById(R.id.tableLayout);
TableRow row = (TableRow)tblLayout.getChildAt(0); // Here get row id depending on number of row
Button button = (Button)row.getChildAt(XXX); // get child index on particular row
String buttonText = button.getText().toString();
Run Code Online (Sandbox Code Playgroud)
3x3格式:(理解实际的代码可能不同)
for(int i=0;i<3;i++)
{
TableRow row = (TableRow)tblLayout.getChildAt(i);
for(int j=0;j<3;j++){
Button button = (Button)row.getChildAt(j); // get child index on particular row
String buttonText = button.getText().toString();
Log.i("Button index: "+(i+j), buttonText);
}
}
Run Code Online (Sandbox Code Playgroud)
你能做的就是找到TableLayout使用的实例
TableLayout layout_tbl = (TableLayout) findViewById(R.id.layout_tbl);
Run Code Online (Sandbox Code Playgroud)
然后通过使用getChildCount()你可以迭代每个孩子的TableLayout和TableRow,也更好地检查View使用,instanceof以便你没有得到任何NPE.
for (int i = 0; i < layout_tbl.getChildCount(); i++) {
View parentRow = layout_tbl.getChildAt(i);
if(parentRow instanceof TableRow){
for (int j = 0; j < parentRow.getChildCount(); j++){
Button button = (Button ) parentRow.getChildAt(j);
if(button instanceof Button){
String text = button.getText().toString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11449 次 |
| 最近记录: |