我需要将android edittext限制为仅允许字母数字字符以及以下字符:空格,“。”,“ _”和“-”。
为此,我使用了以下代码段:
filters[1] = new InputFilter() {
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
if (source instanceof SpannableStringBuilder) {
SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder) source;
for (int i = end - 1; i >= start; i--) {
char currentChar = source.charAt(i);
String strCurrentChar = String.valueOf(currentChar);
if (!Character.isLetterOrDigit(currentChar)
&& !".".equals(strCurrentChar)
&& !"_".equals(strCurrentChar)
&& !" ".equals(strCurrentChar)) {
sourceAsSpannableBuilder.delete(i, i + 1);
}
}
return source;
} else {
StringBuilder filteredStringBuilder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
String strCurrentChar = String.valueOf(currentChar);
if (Character.isLetterOrDigit(currentChar)
|| ".".equals(strCurrentChar)
|| "_".equals(strCurrentChar)
|| " ".equals(strCurrentChar)) {
filteredStringBuilder.append(currentChar);
}
}
return filteredStringBuilder.toString();
}
}
};
它可以很好地限制所有其他字符,但Google-印度键盘中的数学“ pi”符号除外。我如何也可以对此加以限制,以便完全满足要求?