mardi 19 juin 2018

EditText with Specific Pattern

I am trying to code an EditText field to have a specific pattern. The pattern is:

P20_ _ / _ _ / _ _ _ _

What I would like is that when the user starts to type the characters take place of the underscores but the forward slashes remain.

I am a beginner but this seems to be quite challenging.

Here is what I have tried so far which gives me the P20 at the start but the characters does not take place of the underscores:

editPemacNo.setText("P20__/__/____");
    Selection.setSelection(editPemacNo.getText(), editPemacNo.getText().length());


    editPemacNo.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!s.toString().contains("P20")){
                editPemacNo.setText("P20__/__/____");
                Selection.setSelection(editPemacNo.getText(), editPemacNo.getText().length());

            }

        }
    });

Can anyone help me?

Aucun commentaire:

Enregistrer un commentaire