EditText vehicle mask

0

I would like to know how to create a mask in an EditText in ABC-1234 format. I'm trying something like the example below, but with no success yet.

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) {
        }
    };
}

}

    
asked by anonymous 21.08.2018 / 20:10

2 answers

1

I've made an example that can work for 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: link

    
21.08.2018 / 20:24
0

I have a class for this 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 = "###-####"
}}
    
21.08.2018 / 20:19