Mask in TextEdit

2

I have a fuel inlet and I needed a litter mask that had 3 fields after the comma. Example:

  

002,555 Liters

I needed the field to already be filled with "000,000" and filled back as the user typed.

Is it possible to mount a mask in this way?

    
asked by anonymous 09.06.2017 / 15:52

2 answers

1

There is a library developed by jansenfelipe , you can find a tutorial this link

More basically you need to add the following dependency:

dependencies {
  compile 'br.com.jansenfelipe:androidmask:1.0.1'
}

After this, simply create the mask as needed:

EditText cpf = (EditText) findViewById(R.id.txtCPF);
EditText tel = (EditText) findViewById(R.id.txtTelefone);

//Mascara cpf
MaskEditTextChangedListener maskCPF = new MaskEditTextChangedListener("###.###.###-##", cpf);
//Mascara Telefone
MaskEditTextChangedListener maskTEL = new MaskEditTextChangedListener("(##)####-####", tel);

The last step is to add this listener to the field:

cpf.addTextChangedListener(maskCPF);
tel.addTextChangedListener(maskTEL);

With respect to the backfold you can use setGravity to point to where the text starts, for example:

cpf.setGravity(Gravity.RIGHT);
tel.setGravity(Gravity.LEFT);

or xml with the

android:textDirection="rtl"
    
09.06.2017 / 16:23
0

You can do this:

public String mascaraDouble(Double myDouble){
    String result = String.format("%1$,.3f", myDouble);
    return result;
}

String.format is very rich in options, this link has some good examples

    
09.06.2017 / 16:22