Android EditText setError()带有图标,但没有弹出消息
我想对我的EditText进行一些验证,我想在其中显示“”图标(当您放入Android EditText setError()带有图标,但没有弹出消息,android,android-edittext,Android,Android Edittext,我想对我的EditText进行一些验证,我想在其中显示“”图标(当您放入EditText.setError(“废话”)),但不希望弹出窗口中的文本显示“废话” 有什么办法吗?一种方法是创建一个自定义布局,在编辑文本中显示图像图标。但是有更好的解决方案吗?经过大量的研究和排列,问题得到了解决-(也要感谢@van) 创建一个新类,该类将扩展如下内容- public class MyEditText extends EditText { public MyEditText(Context conte
EditText.setError(“废话”))
,但不希望弹出窗口中的文本显示“废话”
有什么办法吗?一种方法是创建一个自定义布局,在编辑文本中显示图像图标。但是有更好的解决方案吗?经过大量的研究和排列,问题得到了解决-(也要感谢@van) 创建一个新类,该类将扩展如下内容-
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);
}
}
<com.raj.poc.MyEditText
android:id="@+id/et_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
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);
}
}
});
在xml中使用此类作为视图,如下所示-
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);
}
}
<com.raj.poc.MyEditText
android:id="@+id/et_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
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\u错误
=
保持文本为空可以解决这个问题
但是如果我们在setError(null)中只保留null,这将不会显示验证错误;它应该与第二个参数一起为null。对不起,Rajkiran,但是您的解决方案在Android 4.2版本上不起作用。如果我试图为错误设置
null
值,它甚至不会显示。我提出的解决方案是扩展EditText
并重写setError
方法。不,我只有错误指示器,没有弹出窗口
@Override
public void setError(CharSequence pError, Drawable pIcon) {
setCompoundDrawables(null, null, pIcon, null);
}
我一直在处理同样的问题。当用户插入空输入时,我想将
.setError()
用于我的EditText
。但我认为弹出消息很烦人,尤其是当你有更多的EditText
s时。
我的解决方案是天真简单的,但它适用于我迄今为止尝试过的所有设备
我创建了自己的类myEditText,只需重写此方法:
@Override
public void setError(CharSequence error, Drawable icon) {
setCompoundDrawables(null, null, icon, null);
}
然后在layout.xml中使用
<cz.project.myEditText
...
/>
当在EditText
上调用setError()
时,它的工作方式与我想要的方式完全相同=无弹出消息您不需要创建新的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字段的错误消息,这非常有用。这是一个非常简单的程序,只需在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");
}
有关更多信息,请参阅此链接:要仅在调用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的字符串,以获取图标和消息弹出窗口。这样,当文本更改时,图标不会消失。@Vincent:请尝试更新的答案。这也适用于4.2,解决了您的问题。谢谢你救了我的命。很好用!只有在setError(“”)时,才能在没有错误消息弹出的情况下获取错误图标代码>被调用,请看下面我的答案:请帮助我,它对我不起作用。。我也应用了同样的方法,一件重要的事情是,我的主题是从“@android:style/theme.Holo.Light.NoActionBar”扩展而来的。因为我和你面临同样的问题,这是我想出的第一个解决办法。可以试试这个。哦,我一定会尝试这个解决方案。如果这在所有android版本上都有效,并且我的答案满足commment#1,我将接受你的答案,而不是我的答案。:)很不幸,你的代码对我不起作用。我不知道为什么。但这确实帮助我优化了答案。请检查我的最新答案。但还是对你的答案投了赞成票。你已经看到了,对吗?请你再看一遍标题好吗?它显示-EditText setError(),带有图标,但没有弹出消息。EditText.setCompoundDrawableSwithinInstincBounds(0,0,R.drawable.error,0)
也可以工作,而且更短。@EmmanuelMess这应该是可以接受的解决方案。你刚刚救了我!谢谢你D