I am using this code so that it only accepts IP addresses in the EditText, but I would like that if I enter more than three digits it will automatically insert the period, could someone tell me how to do this?
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,android.text.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 < splits.length; i++) {
if (Integer.valueOf( splits[i] ) > 255) {
return "";
}
}
}
}
return null;
}
};
ipAddress.setFilters( filters );
The option for you to
EditText
accept IP address type inputs is precisely how you are doing it, by means of aInputFilter
.You must accept up to 3 digits for each segment and period.
example: