Check/Uncheck TableLayout 中的多个复选框

Check/Uncheck multiple checkboxes inside TableLayout

我有一个TableLayout;第一列的第一个TableRow是一个CheckBoxTableLayout 和第一个 TableRow 是在 .axml 文件中创建的。我在 .cs 文件中创建并填充 TableRows 的其余部分。每个以编程方式创建的 TableRow 的第一列也是 CheckBox.

在第一个 TableRow 中使用 CheckBox 我想 Check/Uncheck 所有以编程方式创建的复选框。

在 Windows Forms 应用程序中,我会通过将复选框收集到一个数组中并遍历数组以在每个复选框上设置选中的 属性 来完成此操作。在我的 Android 应用程序中,我不知道该怎么做。

您需要遍历 TableLayout 中的所有 TableRows,找到您的复选框并 checking/unchecking 它们。为此,请将侦听器添加到执行以下操作的第一个复选框的点击事件:

//Number of TableRows
var count = tableLayout.ChildCount;
//Position of you checkbox inside each TableRow
//TODO: make this a constant outside the listener method
var checkBoxPosition = 0;

for (int i = 0; i < count ; i++) {
    var tableRow = tableLayout.GetChildAt(i) as TableRow;
    if (tableRow != null) {
        var checkBox = tableRow .GetChildAt(checkBoxPosition) as CheckBox;
         if (checkBox != null) {
             //TODO: Use state of the main checkbox here
             checkBox.Checked = true;
         } 
    }
}