Sketchware

Formatação para CPF E CNPJ

Jan 13th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. final EditText cpfEditText = (EditText) findViewById(R.id.cpf_edit_text);
  2. cpfEditText.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 cpf = s.toString();
  12.         if (cpf.length() == 11) {
  13.             String cpfFormatado = cpf.substring(0, 3) + "." + cpf.substring(3, 6) + "." + cpf.substring(6, 9) + "-" + cpf.substring(9, 11);
  14.             cpfEditText.setText(cpfFormatado);
  15.             cpfEditText.setSelection(cpfFormatado.length());
  16.         }
  17.     }
  18. });
Add Comment
Please, Sign In to add comment