我使用此过滤器来验证IP地址
localIP.setHintTextColor(Color.parseColor("#aaaaaa"));
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i=0; i
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
localIP.setFilters(filters);
允许的结果是##。##。##。##,我想最多###。###。###。## #即可。我该怎么做?