skip to Main Content

I need the user to type 11 digits but if he types less, zeros appear before the text typed.
Example:

11 digit text:
52396514262

If the user only enters 523:
I want the edit text to appear 00000000523

cpf_formulario_abastecimento.addTextChangedListener(object  : TextWatcher{
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun afterTextChanged(p0: Editable?) {
            for (i in conexaoAPI.listaMotorista!!){
                if (cpf_formulario_abastecimento.text!!.toString() == i.cpf){
                    runOnUiThread {
                        nomeMotoristaHint.text = i.nomeCompleto
                    }
                }
            }
            
        }

    })

2

Answers


  1. Simply

    int num  = 523;
    String formatted = String.format("%011d", num);
    
    Login or Signup to reply.
  2. Change

    if (cpf_formulario_abastecimento.text!!.toString() == i.cpf){
    

    to:

    if (String.format("%011d", Integer(cpf_formulario_abastecimento.text!!.toString())) == i.cpf){
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search