任何Android专家都可以帮忙输入过滤器来忽略这个字符-
吗?
我为此设置了一个类,但是所有字符都被忽略了.....
public class InputFilterReservedCharacters implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
try {
if (end > start) {
for (int index = start; index < end; index++) {
if (source.charAt(index) == "-".toCharArray()[0]) {
return "";
}
}
}
} catch (NumberFormatException nfe) {
}
return "";
}
}
感谢 StoneBird 的有用评论,我希望用户输入除“-”之外的任何内容。我是这样工作的:
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String returnValue = "";
try {
if (end > start) {
for (int index = start; index < end; index++) {
if (source.charAt(index) != '-'){
returnValue = Character.toString(source.charAt(index));
}
}
}
} catch (NumberFormatException nfe) {
}
return returnValue;
}