带有图标但不带有弹出消息的EditText setError()方法

40
我想为我的EditText添加一些验证,其中我想显示“enter image description here”图标(当您放置editText.setError("blah blah"))时,该图标就会出现),但不想在弹出窗口中显示“blah blah”的文本。
有什么解决方法吗?一种方法是创建一个自定义布局,其中将在EditText中显示图像图标。但是是否有更好的解决方案?
6个回答

42

通过大量的研究和排列组合,问题得到了解决 - (同时感谢@van)

创建一个新类,继承EditText,类似于以下内容 -

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);
}
}

可以像这样在xml中使用此类作为视图-

<com.raj.poc.MyEditText
    android:id="@+id/et_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

现在,在第三步中,只需像这样为您的自定义文本视图设置一个TextWatcher-

    et = (MyEditText) findViewById(R.id.et_test);

    errorIcon = getResources().getDrawable(R.drawable.ic_error);
    errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
       et.setError(null,errorIcon);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.toString().length()>6){
                et.setError("", null);
            }else{
                et.setError("", errorIcon);
            }
        }
    });

其中 R.drawable.ic_error =

将文本保持为null可以解决问题。但是,如果我们在 setError(null) 中只保留null,这将不会显示验证错误;它应该与第二个参数一起为null。


2
这样,当文本更改时,图标就不会消失。 - Vincent
@Vincent:请尝试更新后的答案。这个答案在4.2版本上也有效,可以解决你的问题。感谢! - Rajkiran
挽救了我的生命。运作完美! - Akeshwar Jha
要在调用setError("");时获取没有错误消息弹出窗口的错误图标,请参见下面的答案:https://dev59.com/zGox5IYBdhLWcg3ww3CV#44664299 - Micha F.
请帮帮我,这段代码对我不起作用。我已经尝试了相同的方法,但有一个重要的事情需要注意,我的主题是从“@ android:style/Theme.Holo.Light.NoActionBar”继承而来。 - mayur saini

14

您不需要创建新的EditText类或更改XML。解决方案非常简单:

Edittext editText= (EditText) rootView.findViewById(R.id.email);

String str= editText.getText().toString();

if(str.equalsIgnoreCase("") ){

                Drawable dr = getResources().getDrawable(R.drawable.error); 
                                 //add an error icon to yur drawable files
                dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());

                editText.setCompoundDrawables(null,null,dr,null);

            }

9
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error, 0); 也可以使用,而且更短。 翻译:以上代码也可行并且更短。它的作用是在一个EditText控件上添加一个错误图标。 - EmmanuelMess
1
@EmmanuelMess 这应该是被接受的解决方案。你刚刚救了我!谢谢,伙计!:D - Qazi Fahim Farhan

4
抱歉Rajkiran,你的解决方案在Android 4.2版本上不起作用。如果我试图为错误设置null值,它甚至不会显示。我想出的解决方案是扩展EditText并覆盖setError方法。现在我只有错误指示器而没有弹出框。
@Override
public void setError(CharSequence pError, Drawable pIcon) {
    setCompoundDrawables(null, null, pIcon, null);
}

是的。因为我也遇到了和你一样的问题,这是我想出的第一个解决方案。请随意尝试。 - van
哦,我一定会尝试这个解决方案。如果它适用于所有的安卓版本并且满足我的回答中的评论#1,我会接受你的答案而不是我的。 :) - Rajkiran
很遗憾,@van,你的代码对我不起作用。我不知道为什么。但是,它确实帮助我优化了我的答案。请检查我的更新答案。但是,我仍然赞同你的答案。你已经看到了吧? - Rajkiran

1
获取仅错误图标而不弹出错误消息的方法是,只有在调用 setError("")(即将空字符串设置为错误消息),我使用一个自定义的 EditText 类,在其中重写 setError(CharSequence, Drawable) 方法,代码如下:
@Override
public void setError(CharSequence error, Drawable icon) {
    if (error == null) {
        super.setError(null, icon);
        setCompoundDrawables(null, null, null, null);
    }
    else if (error.toString().equals("")) setCompoundDrawables(null, null, icon, null);
    else super.setError(error, icon);
}

其它的都保持不变:

使用setError(null)既不会出现图标,也不会出现信息弹窗。

使用setError(errorMessage),其中errorMessage是至少长度为1的字符串,可以获取图标和信息弹窗。


1
我一直在处理同样的问题。当用户插入 null 输入时,我想使用 .setError() 到我的 EditText 上。但是,我认为弹出的消息很烦人,特别是当你有更多的 EditText 时。 我的解决方案很天真简单,但是迄今为止我尝试过的所有设备都能正常工作。 我创建了自己的类 myEditText,并且只是覆盖了这个方法:
    @Override
    public void setError(CharSequence error, Drawable icon) {
       setCompoundDrawables(null, null, icon, null);
    }

然后在 layout.xml 中使用

    <cz.project.myEditText
     ...
    />

最后,在我的代码中,我将onFocusChangeListener添加到myEditText,这样当有人点击时,图标就会消失。
    myEditText input = (myEditText) findViewById(R.id.input);
    input.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
            input.setError(null);
    });

    if(val == null) input.setError("");

它完全按照我想要的方式工作 = 当在EditText上调用.setError()时没有弹出消息。


-2

这是一个非常有用的程序,当用户输入错误信息时,您可以使用它来显示edittext字段的错误消息。这是一个非常简单的程序,您只需要在edittext中使用serError()方法即可。

步骤1: 创建按钮并实现onclickListener。

btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

步骤2:验证输入字段并在输入字段中设置错误。

if(edName.length()>3){
if(edNumber.length()>3){
Toast.makeText(getApplicationContext(), "Login success", Toast.LENGTH_SHORT).show();
}else{
edNumber.setError("Number Mininum length is 4");
}
}else{
edName.setError("Name Mininum length is 4");
}

请参考此链接了解更多信息:http://velmuruganandroidcoding.blogspot.in/2014/08/set-error-message-in-edittext-android.html


3
请您再次阅读标题,它的意思是 - EditText setError() 带有图标但不显示弹出消息。 - Rajkiran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接