它无法正常工作,因为您首先尝试将TableLayout投射到按钮上…如果TableLayout仅包含按钮,则可以执行以下操作:
TableLayout yourRootLayout = findView....int count = yourRootLayout.getChildCount();for(int i = 0; i < count; i++){ View v = yourRootLayout.getChildAt(i); if(v instanceof TableRow){ TableRow row = (TableRow)v; int rowCount = row.getChildCount(); for (int r = 0; r < rowCount; r++){ View v2 = row.getChildAt(r); if (v2 instanceof Button){ Button b = (Button)v2; b.setonClickListener(this); } } }}并让您的活动实现OnClickListener。只需将您现有的onClick复制到“活动”中…



