How to use TextWatcher with EditText

0

I am trying to implement an app that only accepts hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I have managed to do this separately using TextWatcher separately, but I can not do both functions in a single TextWatcher, please someone could tell me how to do it.

Here are the two TextWatchers:

This TextWatcher only accepts hexadecimal numbers as an edittext entry

          public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (str.isEmpty()) {
                    mTextoEditor2.append(newStr);
                    newStr = "";
                } else if (!str.equals(newStr)) {
                    newStr = str.replaceAll("[^A-F0-9a-f]", "");
                    mTextoEditor2.setText("");
                }
            }

This other inserts a space every four characters entered by edittext

            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (count == 4)
                {
                    mTextoEditor2.setText(mTextoEditor2.getText()+" ");
                    mTextoEditor2.setSelection(mTextoEditor2.getText().length());
                }
            }

I need to join both in one TextWatcher, could someone tell me how to do?

    
asked by W1ll 09.11.2018 в 17:13
source

1 answer

1

You can use this code with the complete string of the edit text.

Remove characters that are not hexadecimal.
Remove space added before.
Recalculate the spaces.
It puts everything in uppercase.

s is the input text and out the output text:

    String s = "ABc1 2369 JRDf89Yt78";

    s = s.replaceAll("[^a-f|A-F|0-9]", "");
    s = s.replaceAll(" ", "");
    String out = "";
    for(int i=0; i<s.length(); i++) {
        if(i>0 && i%4 == 0) {
            out+=" ";
        }
        out+=s.charAt(i);
    }
    out = out.trim().toUpperCase();

out remains as: ABC1 2369 DF89 78 in the example.

The method to use in TextWatcher is afterTextChanged(Editable editable) .

As when changing the text of the EditText in this method, we call back the TextWatecher generating an infinite loop, I add a variable ignorar to TextWatcher and I set it to true when the I am changing text from the method.

etEjemplo.addTextChangedListener(new TextWatcher() {
        private boolean ignorar = false;
        private long ultimaCorrida = -1;
        private String textoOriginal="";

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {

            long estaCorrida = System.currentTimeMillis();
            if (!ignorar) {
                ignorar = true;
                String s = editable.toString();
                //editable.clear();
                if (estaCorrida - ultimaCorrida > 50) {
                    s = s.replaceAll("[^a-f|A-F|0-9]", "");
                    s = s.replaceAll(" ", "");
                    String out = "";
                    for (int i = 0; i < s.length(); i++) {
                        if (i > 0 && i % 4 == 0) {
                            out += " ";
                        }
                        out += s.charAt(i);
                    }
                    out = out.trim().toUpperCase();

                    //editable.append(out);
                    etEjemplo.setText(out);
                    textoOriginal = out;
                    ultimaCorrida = estaCorrida;
                } else {
                    //editable.append(textoOriginal);
                    etEjemplo.setText(textoOriginal);
                }
                etEjemplo.setSelection(etEjemplo.getText().length());
                ignorar = false;
            }
    }
});
    
answered by 09.11.2018 / 17:28
source