Mask in Textedit

Asked

Viewed 255 times

2

I have a form fuel intake and I needed a liter mask that had 3 fields after the comma. Example:

002.555 liters

I needed the field to be filled with "000.000" and that, were filled back to front as the user is typing.

It is possible to assemble a mask in this way?

  • Here is a partial answer, only need to adapt: https://answall.com/a/207432/35406

2 answers

1


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

more basically you need to add the following dependency:

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

After this just 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 Systener to the field:

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

With respect to the filled from back to front you can use the setGravity to point out where the text starts from, for example:

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

or in the xml with the attribute

android:textDirection="rtl"
  • But the one filled back to front ?

  • You can use the .setGravity edited the answer

  • Worked 100%! Thanks!

0

You can do it this way:

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

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

  • But the one filled back to front?

Browser other questions tagged

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