Home > Enterprise >  How to add a character before editText?
How to add a character before editText?

Time:05-03

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
                    }
                }
            }
            
        }

    })

CodePudding user response:

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

to: if (String.format("1d", Integer(cpf_formulario_abastecimento.text!!.toString())) == i.cpf){

CodePudding user response:

Simply

int num  = 523;
String formatted = String.format("1d", num);

CodePudding user response:

You can try using this TextWatcher

  • Related