是否可以使用CheckBox设置`error`字符串?

时间:2018-01-08 16:30:45

标签: android android-checkbox

对于EditText,如果我设置error,则在用户点按EditText时会显示该字符串。但是,如果我在error上设置CheckBox,是否可以阅读?我试过点击红色感叹圈,但是没有显示消息。

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

2 个答案:

答案 0 :(得分:3)

是的,确定你可以这样做,只需在xml中为CheckBox指定这两个属性:

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

enter image description here

修改如上所述,如果您将焦点移至其他视图,则需要两次点击以选中/取消选中CheckBox。一个用于设置焦点的选项卡,第二个用于实际检查/取消选中。一个简单(但不优雅)的解决方案是像这样设置OnFocusChangeListener

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

答案 1 :(得分:1)

是的,这是可能的

checkBox.setError("you error msg");

如果要显示错误消息,请调用

checkBox.requestFocus();
相关问题