/** * Starts to follow text changes in the specified {@link TextView} to format any input. <br/> * IMPORTANT: this call will force watcher to re-create the mask * * @param textView text view to watch and format */ public void installOn(@NonNull final TextView textView) { installOn(textView, false); }
public void refreshMask() { refreshMask(null); }
public void refreshMask(@Nullable final CharSequence initialValue) { final boolean initial = this.mask == null; this.mask = createMask(); checkMask(); final boolean initiationNeeded = initialValue != null; diffMeasures = new DiffMeasures(); if (initiationNeeded) { diffMeasures.setCursorPosition(mask.insertFront(initialValue)); } if ((!initial || initWithMask || initiationNeeded) && isInstalled()) { selfEdit = true; final String formattedInitialValue = mask.toString(); if (textView instanceof EditText) { final Editable editable = (Editable) textView.getText(); editable.replace(0, editable.length(), formattedInitialValue, 0, formattedInitialValue.length()); } else { textView.setText(formattedInitialValue); } setSelection(mask.getInitialInputPosition()); selfEdit = false; } }
setSelection(cursorPosition); callback.onTextFormatted(this, toString());
/** * Starts to follow text changes in the specified {@link TextView} to format any input. * Initial mask's value (e.g. hardcoded head) will be displayed in the view.<br/> * IMPORTANT: this call will force watcher to re-create the mask * * @param textView text view to watch and format */ public void installOnAndFill(@NonNull final TextView textView) { installOn(textView, true); }
/** * @param textView an observable text view which content text will be formatted using * {@link * Mask} * @param initWithMask this flags defines whether hardcoded head of the mask (e.g "+7 ") will * fill the initial text of the {@code textView}. */ protected void installOn(final TextView textView, final boolean initWithMask) { if (textView == null) { throw new IllegalArgumentException("text view cannot be null"); } this.textView = textView; this.initWithMask = initWithMask; // try to remove us from listeners (useful in case user's trying to install the formatter twice on a same TextView) textView.removeTextChangedListener(this); textView.addTextChangedListener(this); this.mask = null; refreshMask(); }