EditText setError()带有图标但没有popup消息

我想对我的EditText进行一些validation,在那里我想显示“ 在这里输入图像描述 “图标(当你把editText.setError("blah blah"))但不希望popup文本显示”等等等等“。

有没有办法做到这一点? 一种方法是创build一个自定义布局,在EditText中显示图像图标。 但有没有更好的解决scheme?

问题解决了大量的研究和排列 – (也感谢@van)

创build一个新的类将扩展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设置为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,这将不会显示validation错误; 它应该与第二个参数一起为空。

您不需要创build一个新的EditText类或更改xml。 解决scheme非常简单:

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

对不起Rajkiran,但你的解决scheme不适用于Android 4.2版本。 如果我试图设置为null值的错误,甚至不显示。 我想出的解决scheme是扩展EditText并覆盖setError方法。 没有我只有错误指示器没有popup。

 @Override public void setError(CharSequence pError, Drawable pIcon) { setCompoundDrawables(null, null, pIcon, null); } 

我一直在处理同样的问题。 我想在用户插入空input时使用.setError()到我的EditText 。 但是我认为popup消息很烦人,特别是当你有更多的EditText 。 我的解决scheme天真简单,但它到目前为止我尝试过的所有设备。

我创build了我自己的类myEditText,只是@Override这个方法:

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

然后在layout.xml使用

  <cz.project.myEditText ... /> 

最后在我的代码中,我把onFocusChangeListenermyEditText ,所以当有人点击时,图标就消失了。

  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()时没有popup消息。

只有当setError("")被调用 (即设置一个空string作为错误消息) 时,才能得到没有错误消息popup的错误图标我使用自定义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)既不能获取图标,也不能获得消息popup窗口。

使用setError(errorMessage) ,其中errorMessage是一个长度至less为1的string,以获取图标和消息popup窗口。

当用户input错误信息时,如果要显示edittext字段的错误消息,这是非常有用的。这是非常简单的程序,只需要在edittext中使用serError()方法。

第1步:创buildbutton并实现onclickListener。

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

步骤2:validationinput字段并在input字段中设置错误。

 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