Check in real time what you have typed in Edittext Android

Asked

Viewed 937 times

2

I have an Edittext that will receive the user’s data. It will click and type a number between 1 to 10. I was wondering if there’s any way I could check once he jumps to another Edittext, if he actually put a number between 1 and 10. Should he put beyond that, come a message warning of this.

The fact check I’m doing so it clicks on the send data button. And only after this task I do the check. Is there a way to do this check before? As I mentioned above?

1 answer

4


Give a setOnFocusChangeListener in his EditText. Ex:

seuEditText.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) { //perdeu o foco
               Integer numero = Integer.parseInt(seuEditText.getText().toString()); //pega o número
               if (numero <=0 && numero >10) { //verifica se está entre 1 e 10
                   Toast.makeText(getApplicationContext(), 
                        "Número incorreto",
                         Toast.LENGTH_LONG).show(); //mostra a msg
                }
            }
        }
    });
  • thank you very much. That’s right, it worked here.

Browser other questions tagged

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