Edittext vehicle mask

Asked

Viewed 345 times

0

I would like to know how to create a mask in an Edittext in ABC-1234 format. I’m trying something as an example below, but still unsuccessful.

public abstract class PlacaVeiculoMask {


public static String PLACA_MASK = "???-####";



public static String unmask(String s) {
    return s.replaceAll("[A-Z]{3}-[0-9]{4}", "");
}

public static TextWatcher insert(final EditText editText) {
    return new TextWatcher() {
        boolean isUpdating;
        String old = "";

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String str = PlacaVeiculoMask.unmask(s.toString());
            String mask = PLACA_MASK;
            String mascara = "";
            if (isUpdating) {
                old = str;
                isUpdating = false;
                return;
            }
            int i = 0;
            for (char m : mask.toCharArray()) {
                if ((m != '#' && str.length() > old.length()) || (m != '#' && str.length() < old.length() && str.length() != i)) {
                    mascara += m;
                    continue;
                }

                try {
                    mascara += str.charAt(i);
                } catch (Exception e) {
                    break;
                }
                i++;
            }

            isUpdating = true;
            editText.setText(mascara);
            editText.setSelection(mascara.length());
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    };
}

}

2 answers

1

I made an example that might suit you.

    public static void main(String[] args) {
        String pattern = "HHH-HHH";
        String numeroProcesso = "ABC123";
        System.out.println(format(pattern, numeroProcesso));
    }

    private static String format(String pattern, Object value) {
        MaskFormatter mask;
        try {
            mask = new MaskFormatter(pattern);
            mask.setValueContainsLiteralCharacters(false);
            return mask.valueToString(value);
        } catch (ParseException e) {


       throw new RuntimeException(e);
    }

Valid characters:

*  qualquer caractere
H  (0-9, a-f or A-F).

For more information: https://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html

0

I have a class for that I used once:

class LicenseVehicleMask(var editText: EditText) : TextWatcher {


private var isUpdating: Boolean = false
protected var mOldString = ""
protected var mMask: String?= ""
internal var befores = ""

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
    befores = s.toString()

}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
    var str = s.toString()

    if (str.length == 0) {
        return
    }

    if (before == 1 && befores.length > 0 && !isUpdating) {
        val last = befores.substring(befores.length, befores.length)
        val rep = last.replace("(", "").replace(")", "").replace(" ", "").replace("-", "")
        if (rep.length == 0) {
            str = str.substring(0, befores.length - 1)
        }
    }

    mMask = DEFAULT_MASK

    val mask = StringBuilder()
    if (isUpdating) {
        mOldString = str
        isUpdating = false
        return
    }
    var i = 0
    for (m in mMask!!.toCharArray()) {
        if (m != '#') {
            mask.append(m)
            continue
        }
        try {
            mask.append(str[i])
        } catch (e: Exception) {
            break
        }

        i++
    }
    isUpdating = true
    val x = mask.toString()
    editText.setText(x)
    editText.setSelection(mask.length)

}

override fun afterTextChanged(s: Editable) {

}

companion object {

    val DEFAULT_MASK = "###-####"
}}
  • Here with me it didn’t work. The only thing I changed was the language, I did this example of code in Java

  • what exactly did not work, the mask did not reach the edittext?

  • My edittext recognizes the mask, enters the function. However it does not format in the correct value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.