Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Deleting characters inside (###) #5 #6

Merged
merged 2 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MaskedFormatter {
private String mInvalidCharacters;
private String mPlaceholderString;
private String mUnMaskedString;
private String mLiterals;
private char mPlaceholder;

// ===========================================================
Expand Down Expand Up @@ -59,6 +60,7 @@ public String getMask() {
public void setMask(String mask) {
mMask = mask;
updateInternalMask();
mLiterals = getNotKeyLiterals();
}

public String getValidCharacters() {
Expand Down Expand Up @@ -112,6 +114,9 @@ public String getUnMaskedString() {
public String valueToString(Object value) {
mUnMaskedString = EMPTY_STRING;
String stringValue = (value == null) ? "" : value.toString();

stringValue = removeLiterals(stringValue);

StringBuffer result = new StringBuffer();
String placeholder = getPlaceholder();
int[] valueCounter = {0};
Expand All @@ -120,6 +125,26 @@ public String valueToString(Object value) {
return result.toString();
}

private String getNotKeyLiterals() {
StringBuilder result = new StringBuilder();
for (MaskCharacter ch : mMaskChars){
if (ch.isLiteral()){
result.append(ch.getChar('0'));
}
}
return result.toString();
}

private String removeLiterals(String value) {
StringBuilder result = new StringBuilder();
for (char ch : value.toCharArray()) {
if (mLiterals.indexOf(ch) == -1) {
result.append(ch);
}
}
return result.toString();
}

private void updateInternalMask() {
ArrayList<MaskCharacter> fixed = new ArrayList<MaskCharacter>();
ArrayList<MaskCharacter> temp = fixed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.text.TextWatcher;

public class MaskedWatcher implements TextWatcher {
private String oldInput;

// ===========================================================
// Constants
Expand Down Expand Up @@ -52,13 +53,18 @@ public String getUnMaskedString() {
public void afterTextChanged(Editable s) {
String filtered = mMaskFormatter.valueToString(s);

if (s.length() > filtered.length() && s.length() > oldInput.length()) {
filtered = mMaskFormatter.valueToString(oldInput);
}

if (!TextUtils.equals(s, filtered)) {
s.replace(0, s.length(), filtered);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldInput = s.toString();
}

@Override
Expand Down