From f7953b5c74fb5a5e1c66b35f1f1e12761b201f83 Mon Sep 17 00:00:00 2001 From: Leonardo Javier Esparis Meza Date: Sat, 25 Mar 2017 17:55:54 -0400 Subject: [PATCH] now when nothing is inside a edittext, hint should be there the instead a text, when users delete all the phone number, international code text should be replaced with a international code hint --- .../phoneedittext/PhoneEditText.java | 61 +++++++------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/phoneedittext/src/main/java/com/github/leoxnidas/phoneedittext/PhoneEditText.java b/phoneedittext/src/main/java/com/github/leoxnidas/phoneedittext/PhoneEditText.java index 12319cb..1ba80a6 100644 --- a/phoneedittext/src/main/java/com/github/leoxnidas/phoneedittext/PhoneEditText.java +++ b/phoneedittext/src/main/java/com/github/leoxnidas/phoneedittext/PhoneEditText.java @@ -96,46 +96,31 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - // if user intent to write a number - // at international code space, - // that number shall be added next - // to the international code - // of the string, automatically - // - // Example: - // - // user intents to write this - // - // --------------- - // | +518 | - // -------------- - // - // and the result shall be this - // - // --------------- - // | +581 | - // -------------- - // String currentText = s.toString(); int currentLength = currentText.length(); String code = PLUS + p_mCodeStr; int codeLength = code.length(); - if(currentLength < codeLength) - return; - - for(int i = 0; i < codeLength; i++) { - if(currentText.charAt(i) != code.charAt(i)) { - String nCode = code; - - for(int j = codeLength + 1; j < currentLength; j++) { - nCode += Character.toString(currentText.charAt(j)); + if(currentLength < codeLength && p_mLastLenght == 0) { + setText((code + s)); + } else { + try { + for(int i = 0; i < codeLength; i++) { + if(currentText.charAt(i) != code.charAt(i)) { + String nCode = code; + + for(int j = codeLength + 1; j < currentLength; j++) { + nCode += Character.toString(currentText.charAt(j)); + } + + setText(nCode); + Selection.setSelection(getText(), i); + break; + } } + } catch (StringIndexOutOfBoundsException ignored) { - nCode += Character.toString(currentText.charAt(i)); - setText(nCode); - break; } } } @@ -143,17 +128,13 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { @Override public void afterTextChanged(Editable s) { if(s.length() == (PLUS + p_mCodeStr).length() - 1 - && p_mLastLenght > (PLUS + p_mCodeStr).length() - 1) { + && p_mLastLenght > (PLUS + p_mCodeStr).length() - 1) setText(""); - } else { - p_mLastLenght = s.length(); + if(p_mLastLenght <= (PLUS + p_mCodeStr).length()) + Selection.setSelection(getText(), getText().length()); - if(p_mLastLenght == 1) { - setText((PLUS + p_mCodeStr + s.toString())); - Selection.setSelection(getText(), getText().length()); - } - } + p_mLastLenght = getText().length(); } }