【问题标题】:How to remove the Error message box from setError in android?如何从android中的setError中删除错误消息框?
【发布时间】:2016-02-01 09:53:42
【问题描述】:

默认情况下,setError 使EditText 如下所示:

有一个红色感叹号。当特定的EditText 获得焦点时会弹出错误消息。

我正在创建一个自定义的setError 方法:

validatedIcon = ContextCompat.getDrawable(getApplicationContext(), R.drawable.validated);
validatedIcon.setBounds(0, 0, validatedIcon.getIntrinsicWidth(), validatedIcon.getIntrinsicHeight());
firstName.setError("Please check your first name",validatedIcon);

我的新错误图标是自定义图标。但我不希望错误消息与它一起显示。我尝试将 null 作为消息传递,但这删除了整个图标。我尝试传递空字符串,但这只是产生了一个空框。

尝试过的替代方案:我尝试使用setCompoundDrawablesWithIntrinsicBounds 设置图标,但即使经过多次拉扯,它也无法正常工作。

那么,有没有办法摆脱setError 中的消息框?

【问题讨论】:

标签: android


【解决方案1】:

正如 Patel Hiren 在评论中建议的那样,创建一个扩展 EditText 的自定义类并覆盖 setError 方法解决了我的问题:

public class MyEditText extends EditText {

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setError(CharSequence error, Drawable icon) {
        setCompoundDrawables(null, null, icon, null);
    }
}

此外,在我的布局文件中,我必须将 EditText 替换为此类的完全限定名称(packagename.className)。效果很好。

【讨论】:

    【解决方案2】:

    你可以设置你想要的位置 firstName.setError(null);

    【讨论】:

    • 这也会删除错误符号。我只想删除错误文本,而不是符号。
    • 您只想正确地错误符号。你可以试试这个 public class MyEditText extends EditText { public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setError(CharSequence error, Drawable icon) { setCompoundDrawables(null, null, icon, null); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    相关资源
    最近更新 更多