是否可以使用 CheckBox 设置“错误”字符串?

Is it possible to set the `error` string with CheckBox?

对于EditText,如果我设置error,当用户点击EditText时显示字符串。但是如果我在一个CheckBox上设置error,它能被读取吗?我尝试点击红色感叹号圈,但没有显示消息。

myCheckBox.error = "Can the user read this error message?"

是的,当然可以,只需在 xml 中为 CheckBox 指定这两个属性:

 android:focusableInTouchMode="true"
 android:focusable="true"

编辑: 如您所述,如果将焦点移动到其他视图,则需要点击两次才能到达 check/uncheck CheckBox。一个用于设置焦点的选项卡,仅次于 check/uncheck。一种简单(但并不优雅)的解决方案是像这样设置 OnFocusChangeListener

        chBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    chBox.setChecked(!chBox.isChecked());
                }
            }
        });

是的,有可能

checkBox.setError("you error msg");

当您希望显示错误消息时调用

checkBox.requestFocus();