Java Code Examples for android.text.Editable#setFilters()

The following examples show how to use android.text.Editable#setFilters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditMessage.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onTextContextMenuItem(int id) {
    if (id == android.R.id.paste) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
        } else {
            Editable editable = getEditableText();
            InputFilter[] filters = editable.getFilters();
            InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
            if (filters != null) {
                System.arraycopy(filters, 0, tempFilters, 1, filters.length);
            }
            tempFilters[0] = SPAN_FILTER;
            editable.setFilters(tempFilters);
            try {
                return super.onTextContextMenuItem(id);
            } finally {
                editable.setFilters(filters);
            }
        }
    } else {
        return super.onTextContextMenuItem(id);
    }
}
 
Example 2
Source File: ValorMonetarioWatcher.java    From canarinho with Apache License 2.0 6 votes vote down vote up
private void atualizaTexto(Editable editable, String valor) {
    mudancaInterna = true;

    final InputFilter[] oldFilters = editable.getFilters();

    editable.setFilters(new InputFilter[] {});
    editable.replace(0, editable.length(), valor);

    editable.setFilters(oldFilters);

    if (valor.equals(editable.toString())) {
        // TODO: estudar implantar a manutenção da posição do cursor
        Selection.setSelection(editable, valor.length());
    }

    mudancaInterna = false;
}
 
Example 3
Source File: MascaraNumericaTextWatcher.java    From canarinho with Apache License 2.0 6 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {

    // retorna se a mudança foi disparada pelo método atualizaTexto
    if (isMudancaInterna()) {
        return;
    }

    // Filtro de tamanho
    if (!Arrays.equals(s.getFilters(), filtroNumerico)) {
        s.setFilters(filtroNumerico);
    }

    final StringBuilder builder = trataAdicaoRemocaoDeCaracter(s, mascara);

    atualizaTexto(validador, resultadoParcial, s, builder);
}
 
Example 4
Source File: SafePasteEditText.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTextContextMenuItem(int id) {
	if (id == android.R.id.paste) {
		Editable editable = getEditableText();
		InputFilter[] filters = editable.getFilters();
		InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
		if (filters != null) {
			System.arraycopy(filters, 0, tempFilters, 1, filters.length);
		}
		tempFilters[0] = SPAN_FILTER;
		editable.setFilters(tempFilters);
		try {
			return super.onTextContextMenuItem(id);
		} finally {
			editable.setFilters(filters);
		}
	} else {
		return super.onTextContextMenuItem(id);
	}
}
 
Example 5
Source File: EditMessage.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onTextContextMenuItem(int id) {
    if (id == android.R.id.paste) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
        } else {
            Editable editable = getEditableText();
            InputFilter[] filters = editable.getFilters();
            InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
            if (filters != null) {
                System.arraycopy(filters, 0, tempFilters, 1, filters.length);
            }
            tempFilters[0] = SPAN_FILTER;
            editable.setFilters(tempFilters);
            try {
                return super.onTextContextMenuItem(id);
            } finally {
                editable.setFilters(filters);
            }
        }
    } else {
        return super.onTextContextMenuItem(id);
    }
}
 
Example 6
Source File: CPFCNPJTextWatcher.java    From canarinho with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(final Editable s) {

    if (isMudancaInterna()) {
        return;
    }

    s.setFilters(FILTRO_CPF_CNPJ);

    final char[] mascara = ehCpf(s) ? CPF : CNPJ;
    final StringBuilder builder = trataAdicaoRemocaoDeCaracter(s, mascara);
    atualizaTexto(validador, resultadoParcial, s, builder);
}
 
Example 7
Source File: BoletoBancarioTextWatcher.java    From canarinho with Apache License 2.0 5 votes vote down vote up
private void verificaFiltro(final Editable s, final boolean tributo) {
    // Filtro de tamanho
    if (tributo && !Arrays.equals(s.getFilters(), FILTRO_TRIBUTO)) {
        s.setFilters(FILTRO_TRIBUTO);
    } else if (!tributo && !Arrays.equals(s.getFilters(), FILTRO_NORMAL)) {
        s.setFilters(FILTRO_NORMAL);
    }
}
 
Example 8
Source File: TelefoneTextWatcher.java    From canarinho with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {

    if (isMudancaInterna()) {
        return;
    }

    s.setFilters(FILTRO_NOVE_DIGITOS);

    final char[] mascara = ehNoveDigitos(s) ? TELEFONE_NOVE_DIGITOS : TELEFONE_OITO_DIGITOS;
    final StringBuilder builder = trataAdicaoRemocaoDeCaracter(s, mascara);

    atualizaTexto(validador, resultadoParcial, s, builder);
}
 
Example 9
Source File: ViewUtils.java    From PaymentKit-Droid with Apache License 2.0 5 votes vote down vote up
public static void replaceAllText(Editable editable, CharSequence newString) {
    InputFilter[] filters = editable.getFilters();
    editable.setFilters(new InputFilter[] { });
    // We need to remove filters so we can add text with spaces.
    editable.replace(0, editable.length(), newString);
    editable.setFilters(filters);
}
 
Example 10
Source File: FormattedEditText.java    From FormatEditText with MIT License 4 votes vote down vote up
private void formatTextWhenDelete(final Editable editable, int start, int before) {
    mIsFormatted = true;
    final boolean filter = mFilterRestoreTextChangeEvent;
    super.removeTextChangedListener(mTextWatcher);
    InputFilter[] filters = editable.getFilters();
    editable.setFilters(EMPTY_FILTERS);
    int selectionStart, selectionEnd;
    if (!filter) {
        selectionStart = Selection.getSelectionStart(editable);
        selectionEnd = Selection.getSelectionEnd(editable);
        editable.setSpan(SELECTION_SPAN, selectionStart, selectionEnd, Spanned.SPAN_MARK_MARK);
    }
    if (mMode < MODE_MASK) {
        final boolean deletedLast = start >= editable.length();
        if (!deletedLast) {
            formatDefined(editable, start, true);
        } else {
            for (int i = start; i > 0; i--) {
                final char sub = editable.charAt(i - 1);
                final char place = findPlaceholder(i - 1);
                if (sub == place) {
                    editable.delete(i - 1, i);
                } else {
                    break;
                }
            }
        }
    } else {
        formatMask(editable, start, true);
    }
    if (!filter) {
        selectionStart = editable.getSpanStart(SELECTION_SPAN);
        selectionEnd = editable.getSpanEnd(SELECTION_SPAN);
        editable.removeSpan(SELECTION_SPAN);
        editable.setFilters(filters);
        Editable text = getText();
        Selection.setSelection(text, selectionStart, selectionEnd);
    } else {
        setFilters(filters);
    }
    mIsFormatted = false;
    super.addTextChangedListener(mTextWatcher);
}
 
Example 11
Source File: FormattedEditText.java    From FormatEditText with MIT License 4 votes vote down vote up
private void formatTextWhenAppend(final Editable editable, int start, int before, int count) {
    mIsFormatted = true;
    final boolean filter = mFilterRestoreTextChangeEvent;
    super.removeTextChangedListener(mTextWatcher);
    InputFilter[] filters = editable.getFilters();
    editable.setFilters(EMPTY_FILTERS);
    int selectionStart, selectionEnd;
    if (!filter) {
        selectionStart = Selection.getSelectionStart(editable);
        selectionEnd = Selection.getSelectionEnd(editable);
        editable.setSpan(SELECTION_SPAN, selectionStart, selectionEnd, Spanned.SPAN_MARK_MARK);
    }
    if (mMode < MODE_MASK) {
        boolean appendedLast = start > mHolders[mHolders.length - 1].index;
        if (!appendedLast) {
            formatDefined(editable, start, false);
        }
    } else {
        formatMask(editable, start, false);
    }
    if (!filter) {
        selectionStart = editable.getSpanStart(SELECTION_SPAN);
        selectionEnd = editable.getSpanEnd(SELECTION_SPAN);
        editable.removeSpan(SELECTION_SPAN);
        editable.setFilters(filters);
        if (mLengthFilterDelegate != null) {
            CharSequence out =
                    mLengthFilterDelegate.mFilter.filter(
                            editable, 0, editable.length(), EMPTY_SPANNED, 0, 0);
            if (out != null) {
                editable.delete(out.length(), editable.length());
            }
        }
        Selection.setSelection(
                editable,
                Math.min(selectionStart, editable.length()),
                Math.min(selectionEnd, editable.length()));
    } else {
        editable.setFilters(filters);
    }
    mIsFormatted = false;
    super.addTextChangedListener(mTextWatcher);
}
 
Example 12
Source File: SolveTimeNumberTextWatcher.java    From TwistyTimer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    if (isFormatting)
        return;

    isFormatting = true;

    // Since the keyboard input type is "number", we can't punctuation with the actual
    // filters, so we clear them and restore once we finish formatting
    InputFilter[] filters = s.getFilters(); // save filters
    s.setFilters(new InputFilter[] {});     // clear filters


    // Clear all formatting from editable
    // Regex matches the characters ':', '.', 'h' and a leading zero, if present
    mUnformatted = s.toString().replaceAll("^0+|[h]|:|\\.", "");
    mLen = mUnformatted.length();

    s.clear();
    s.insert(0, mUnformatted);


    if (mLen <= 2 && mLen > 0) { // 12 -> 0.12
        s.insert(0, "0.");
    } else if (mLen == 3) { // 123 -> 1.23
        s.insert(1, ".");
    } else if (mLen == 4) { // 1234 -> 12.34
        s.insert(2, ".");
    } else if (mLen == 5) { // 12345 -> 1:23.45
        s.insert(1, ":");
        s.insert(4, ".");
    } else if (mLen == 6) { // 123456 -> 12:34.56
        s.insert(2, ":");
        s.insert(5, ".");
    } else if (mLen == 7) { // 1234567 -> 1:23:45.67
        s.insert(1, "h");
        s.insert(4, ":");
        s.insert(7, ".");
    } else if (mLen == 8) { // 12345678 -> 12:34:56.78
        s.insert(2, "h");
        s.insert(5, ":");
        s.insert(8, ".");
    }

    isFormatting = false;

    // Restore filters
    s.setFilters(filters);
}
 
Example 13
Source File: MaskedEditText.java    From FormattEditText with Apache License 2.0 4 votes vote down vote up
private void formatMask(@NonNull Editable value) {
	InputFilter[] inputFilters = value.getFilters();
	value.setFilters(new InputFilter[0]);

	int indexInMask = 0;
	int indexInText = 0;
	int maskLength = 0;
	int inputLength = 0;
	boolean treatNextCharAsLiteral = false;
	boolean maskIsNotNumeric = false;

	Object selection = new Object();
	value.setSpan(selection, Selection.getSelectionStart(value), Selection.getSelectionEnd(value), Spanned.SPAN_MARK_MARK);

	while (indexInMask < mask.length()) {
		char charInMask = mask.charAt(indexInMask);
		if (!treatNextCharAsLiteral && isMaskChar(charInMask)) {
			// Found mask character, try to parse text
			maskIsNotNumeric |= charInMask != NUMBER_MASK;
			if (indexInText >= value.length()) {
				// Add trailing placeholders
				value.insert(indexInText, placeholder);
				value.setSpan(new PlaceholderSpan(), indexInText, indexInText + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				++indexInText;
				++inputLength;
				++maskLength;
				++indexInMask;
			} else if (!matchMask(charInMask, value.charAt(indexInText))) {
				// Skip bad character in text
				value.delete(indexInText, indexInText + 1);
			} else {
				// Character in text is acceptable, go to next character in mask
				++indexInText;
				++inputLength;
				++maskLength;
				++indexInMask;
			}
		} else if (!treatNextCharAsLiteral && charInMask == ESCAPE_CHAR) {
			// Next character in mask must be escaped
			treatNextCharAsLiteral = true;
			++indexInMask;
		} else {
			// Found a literal or escaped character in mask
			value.insert(indexInText, String.valueOf(charInMask));
			value.setSpan(new LiteralSpan(), indexInText, indexInText + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			treatNextCharAsLiteral = false;

			++indexInText;
			++maskLength;
			++indexInMask;
		}
	}

	while (value.length() > maskLength) {
		int pos = value.length() - 1;
		value.delete(pos, pos + 1);
	}

	Selection.setSelection(value, value.getSpanStart(selection), value.getSpanEnd(selection));
	value.removeSpan(selection);

	value.setFilters(inputFilters);

	int newInputType = inputLength > 0
			? (maskIsNotNumeric
			? InputType.TYPE_CLASS_TEXT
			: InputType.TYPE_CLASS_NUMBER)
			: 0;
	if (getInputType() != newInputType)
		setInputType(newInputType);
}
 
Example 14
Source File: CEPTextWatcher.java    From canarinho with Apache License 2.0 3 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {

    if (isMudancaInterna()) {
        return;
    }

    s.setFilters(FILTRO_OITO_DIGITOS);

    final StringBuilder builder = trataAdicaoRemocaoDeCaracter(s, CEP_DIGITOS);

    atualizaTexto(validador, resultadoParcial, s, builder);
}