Advertisement
Sketchware

Limite caracteres no edittext

Jan 13th, 2023
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.72 KB | None | 0 0
  1. EditText edittext5 = (EditText) findViewById(R.id.edittext5);
  2. InputFilter[] editFilters = edittext5.getFilters();
  3. InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
  4. System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
  5. newFilters[editFilters.length] = new InputFilter.LengthFilter(2) {
  6.     @Override
  7.     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  8.         if (dest.length() >= 2) {
  9.             Toast.makeText(HomeActivity.this, "Limite de caracteres é 2", Toast.LENGTH_SHORT).show();
  10.             return "";
  11.         }
  12.         return super.filter(source, start, end, dest, dstart, dend);
  13.     }
  14. };
  15. edittext5.setFilters(newFilters);
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement