mycheckbox.setSelected(false) 无效

mycheckbox.setSelected(false) has no effect

我有一列复选框,其中最上面一行是该特定列的 CheckAll 复选框。如果我取消选中最左侧列中第一个 CheckAll 复选框中的 Checkall,我想取消选中其余的 CheckAll 复选框。

但是mycheckbox.setSelected(false)没有效果。但是,如果我做了 mycheckbox.setEnabled(false)(只是作为测试)它确实有效并且复选框被禁用。

顺便说一下,这是一个带有自定义适配器的列表视图 "header row"。列表视图的内容按预期工作。

知道如何取消选中复选框吗?

您应该使用 mycheckbox.setChecked(false) 而不是 setSelected

我尝试搜索 setSelected 以查看它的作用,但在 CheckBox 的 official documentation 中,我找不到它(这向我暗示此方法可能是在 CheckBoxparent classes 之一中找到)。尝试在 Android Studio 中输入:

CheckBox cb = new CheckBox(getApplication());
cb.setSelected(true);

转到方法的实现 (CTRL+Click) 并在 TextView class 中看到了这一点,几乎所有其他小部件都是从中派生的:

@Override
public void setSelected(boolean selected) {
    boolean wasSelected = isSelected();

    super.setSelected(selected);

    if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
        if (selected) {
            startMarquee();
        } else {
            stopMarquee();
        }
    }
}

Interesting thing to note here is that setChecked method is contained in the CompoundButton class, while setSelected is is TextView. That means that setSelected does something completely different because textView surely cannot be checked/unchecked.

我希望这解释得很好。