Advertisement
Sketchware

Formatação CPF

Jan 13th, 2023
991
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.74 KB | None | 0 1
  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. });
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement