InputFilter[] filters = new InputFilter[1]; filters[0] = new InputFilter.LengthFilter(10); EditText editText = findViewById(R.id.edit_text); editText.setFilters(filters);
InputFilter[] filters = new InputFilter[1]; filters[0] = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (!Character.isDigit(source.charAt(i))) { return ""; } } return null; } }; EditText editText = findViewById(R.id.edit_text); editText.setFilters(filters);In this example, we have created a new InputFilter implementation to only allow numerical input. The implementation uses the Character.isDigit() method to determine if the entered character is a digit or not. If a non-digit character is entered, the filter returns an empty string, effectively ignoring the input. The android.widget.EditText setFilters() method belongs to the android.widget package library.