Skip to content
Snippets Groups Projects

Password retrieval layout

Merged Lucas Braz Cunha requested to merge recuperar_senha into develop
40 files
+ 574
166
Compare changes
  • Side-by-side
  • Inline
Files
40
@@ -9,38 +9,80 @@ import android.text.TextWatcher;
import android.widget.EditText;
public abstract class Mask {
/**
* Remove all symbols from masked string
*/
public static String unmask(String s) {
/**
* Remove all symbols
*/
return s.replaceAll("[.]", "").replaceAll("[-]", "")
.replaceAll("[/]", "").replaceAll("[(]", "")
.replaceAll("[)]", "");
}
private static boolean isMaskSymbol(char c){
return c == '.' || c == '-' || c == '/' || c == '(' || c == ')';
}
/**
* Create the mask
* REF: https://nuvemandroid.wordpress.com/2013/12/13/aplicacao-de-mascaras-em-formularios/
*/
public static TextWatcher insert(final String mask, final EditText ediTxt) {
return new TextWatcher() {
boolean isUpdating;
boolean isUpdating = false;
boolean isDeleting = false;
boolean reMask = false;
int pos = 0;
String old = "";
public void onTextChanged(CharSequence s, int start, int before,int count) {
String str = Mask.unmask(s.toString());
String mask2 = "";
if(!isUpdating && (count < before) || (start + count) < s.length()){
isDeleting = true;
pos = start + count;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(count == 1 && after == 0 && isMaskSymbol(s.charAt(start))){
reMask = true;
}
}
public void afterTextChanged(Editable s) {
StringBuilder str = new StringBuilder(Mask.unmask(s.toString()));
StringBuilder checkMaskLastPos;
if (isUpdating) {
old = str;
old = str.toString();
isUpdating = false;
return;
}
StringBuilder mask2 = new StringBuilder("");
//if last position is a character mask related, remove it.
if(reMask && s.toString().length() > 0) {
checkMaskLastPos = new StringBuilder(s.toString());
if (isMaskSymbol(checkMaskLastPos.charAt(checkMaskLastPos.length() - 1))) {
checkMaskLastPos.deleteCharAt(checkMaskLastPos.length() - 1);
str = new StringBuilder(unmask(checkMaskLastPos.toString()));
}
}
int i = 0;
if( str.length() > old.length()){
if( reMask || str.length() > old.length()){
for (char m : mask.toCharArray()) {
if (m != '#') {
mask2 += m;
mask2.append(m);
}else{
mask2.append(str.charAt(i));
try {
mask2 += str.charAt(i);
//stop putting the mask when user string ends
str.charAt(i+1);
} catch (Exception e) {
break;
}
@@ -48,14 +90,24 @@ public abstract class Mask {
}
}
}else{
mask2 = s.toString();
mask2.append(s.toString());
}
//has to be set before setting text to prevent infinite recursion.
isUpdating = true;
ediTxt.setText(mask2);
ediTxt.setSelection(mask2.length());
ediTxt.setText(mask2.toString());
if(!isDeleting) {
ediTxt.setSelection(mask2.length());
}
else {
ediTxt.setSelection(pos);
}
reMask = false;
isDeleting = false;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
};
}
}
\ No newline at end of file
Loading