Android:处理InputFilter上的退格键 [英] Android: Handle backspace on InputFilter
本文介绍了Android:处理InputFilter上的退格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为EditText组件创建了一个InputFilter,该组件仅允许在一定范围内(例如1.5到5.5)加倍.一切正常,直到我删除了小数点:
I created an InputFilter for an EditText component that only allows doubles within a range (e.g. from 1.5 to 5.5). Everything worked fine until I deleted the decimal point:
我键入了1.68,然后删除了小数点.文本字段中的值变为168,显然超出了范围.
I typed 1.68 and then deleted the decimal point. The value in the text field became 168, which is obviously outside the range.
这是我的过滤器的简化版本
Here is a simplified version of my filter
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (isValid(dest.toString() + source.toString())) {
//input is valid
return null;
}else{
//The input is not valid
return "";
}
}
private boolean isValid(String input) {
Double inputValue = Double.parseDouble(input);
boolean isMinValid = (1.5 <= inputValue);
boolean isMaxValid = (5.5 >= inputValue);
return isMinValid && isMaxValid;
}
推荐答案
我解决了我的问题.如果有人需要它,这是解决方案:
I solved my problem. Here is the solution in case someone else needs it:
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (isValid(dest.toString() + source.toString())) {
//input is valid
return null;
}else{
//The input is not valid
if (source.equals("") && dest.toString().length() != 1) {
//backspace was clicked, do not accept that change,
//unless user is deleting the last char
CharSequence deletedCharacter = dest.subSequence(dstart, dend);
return deletedCharacter;
}
return "";
}
}
这篇关于Android:处理InputFilter上的退格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文