我尝试了很多并做过R& D,但无法在EditText下显示自己的自定义错误。
这是我的代码:
String str = "Please enter a valid email address”;
// This line is used to give color to error text.
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.YELLOW);
// This line is used to give background color to error text.
BackgroundColorSpan backColorSpan = new BackgroundColorSpan(Color.BLUE);
// This s basically a StringBuilder that implements CharSequence interface.
SpannableStringBuilder strBuilder = new SpannableStringBuilder(str);
strBuilder.setSpan(colorSpan, 0, str.length(), 0);
EditText et = (EditText)findViewById(R.id.et);
Drawable drawable = getResources().getDrawable(R.drawable.btn);
// This line shows our own icon in place of default icon.
setError(CharSequence, Drawable);
So, strBuilder in next line is an implementation of CharSequence.
et.setError(strBuilder, drawable);
任何解决方案?
答案 0 :(得分:0)
您可以使用TextInputLayout,它内置了此功能。
检查此链接,它正是您所需要的:
http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
有关详细信息,请查看Google开发者
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
答案 1 :(得分:0)
我找到了解决方案。
您需要在drawable对象上调用setBound()方法。 你需要把这一行:
drawable.setBounds(0,0,drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());