Advertisement
Sketchware

ESSE CODIGO FORMATA A PLACA E JA LIMITA OS CARACTERES

Jan 15th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. final EditText plateEditText = (EditText) findViewById(R.id.plate_edit_text);
  2. plateEditText.addTextChangedListener(new TextWatcher() {
  3.     @Override
  4.     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  5.     }
  6.     @Override
  7.     public void onTextChanged(CharSequence s, int start, int before, int count) {
  8.     }
  9.     @Override
  10.     public void afterTextChanged(Editable s) {
  11.         final String plate = s.toString();
  12.         if (plate.length() == 8) {
  13.             Toast.makeText(getApplicationContext(), "Plate number has reached 8 characters!", Toast.LENGTH_SHORT).show();
  14.         }
  15.         if (plate.length() > 8) {
  16.             s.delete(8, s.length());
  17.         }
  18.         if (plate.length() == 7) {
  19.             final String plateFormatted = plate.substring(0, 3) + "-" + plate.substring(3, 7);
  20.             plateEditText.setText(plateFormatted);
  21.             plateEditText.setSelection(plateFormatted.length());
  22.         }
  23.     }
  24. });
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement