Ego*_*gor 3 android android-edittext
我的想法是错误设置View为EditText当最大字符限制已经达到.是否有关于此事件的回调,或者可能有另一种方法来实现此效果?提前致谢.
EditText中的maxLength属性实际上是一个InputFilter,您可以自己编写并从代码中提供.
您可以查看InputFilter.LengthFilter的实现,如果没有溢出,它基本上返回null.(请参阅http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/text/InputFilter.java#InputFilter.LengthFilter.filter%28java. lang.CharSequence%2Cint%2Cint%2Candroid.text.Spanned%2Cint%2Cint%29)
您可以为InputFilter.LengthFilter创建一个扩展,在该扩展中调用super并将其与null进行比较以确定是否需要显示警报.
editText.setInputFilters(new InputFilter[] {
new InputFilter.LengthFilter(max) {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
CharSequence res = super.filter(source, start, end, dest, dstart, dend);
if (res != null) { // Overflow
editText.setError("Overflow");
}
return res;
}
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用编辑文本setError:
editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() > max)
editText.setError("Error");
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3719 次 |
| 最近记录: |