Advertisement
Sketchware

Formatação para celular

Jan 21st, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. final EditText phoneEditText = (EditText) findViewById(R.id.phone_edit_text);
  2. Button formatButton = (Button) findViewById(R.id.format_button);
  3.  
  4. formatButton.setOnClickListener(new View.OnClickListener() {
  5. @Override
  6. public void onClick(View view) {
  7. String phoneNumber = phoneEditText.getText().toString();
  8. String formattedPhoneNumber = "";
  9.  
  10. if (phoneNumber.length() == 11) {
  11. formattedPhoneNumber = "(" + phoneNumber.substring(0, 2) + ") " + phoneNumber.substring(2, 6) + "-" + phoneNumber.substring(6);
  12. } else {
  13. // Número de celular não está no formato correto
  14. }
  15.  
  16. phoneEditText.setText(formattedPhoneNumber);
  17. }
  18. });
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement