Created
March 23, 2017 06:30
-
-
Save Firsto/58dace88e4871221164196a8a9543319 to your computer and use it in GitHub Desktop.
textwatcher to format russian phone numbers like +7 800 555 4444
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.firsto.testtesttest.ui.activity.auth; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
/** | |
* @author razor | |
* @created 23.03.17 | |
**/ | |
public class AwesomeUniversalUltimatePhoneTextWatcher implements TextWatcher { | |
public static final char SPACE_CHAR = ' '; | |
public static final String SPACE = " "; | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@Override | |
public synchronized void afterTextChanged(Editable s) { | |
if (s.length() > 0 && !s.toString().startsWith("+")) s.insert(0, "+"); | |
if (s.length() > 1 && !s.toString().startsWith("+7")) s.insert(1, "7"); | |
if (s.length() > 2 && (s.charAt(2) != SPACE_CHAR)) s.insert(2, SPACE); | |
if (s.length() > 6 && (s.charAt(6) != SPACE_CHAR)) s.insert(6, SPACE); | |
if (s.length() > 10 && (s.charAt(10) != SPACE_CHAR)) s.insert(10, SPACE); | |
if (s.length() > 15) s.replace(0, s.length(), s.toString().substring(0, 15)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment