通过大量的研究和排列组合,问题得到了解决 - (同时感谢@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。
您不需要创建新的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);
}
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error, 0);
也可以使用,而且更短。 翻译:以上代码也可行并且更短。它的作用是在一个EditText
控件上添加一个错误图标。 - EmmanuelMessnull
值,它甚至不会显示。我想出的解决方案是扩展EditText
并覆盖setError
方法。现在我只有错误指示器而没有弹出框。@Override
public void setError(CharSequence pError, Drawable pIcon) {
setCompoundDrawables(null, null, pIcon, null);
}
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的字符串,可以获取图标和信息弹窗。
.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()
时没有弹出消息。
这是一个非常有用的程序,当用户输入错误信息时,您可以使用它来显示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
setError("");
时获取没有错误消息弹出窗口的错误图标,请参见下面的答案:https://dev59.com/zGox5IYBdhLWcg3ww3CV#44664299 - Micha F.