Perform an action after each character typed in an Edittext

Asked

Viewed 641 times

0

The thing is, I’m having trouble finding out if the user typed something into EditorText. What I want is that, each character, number or letter that the user type, I can perform an action, soon after.

I researched a lot, I did several tests but could not because I am new in android. From what I understand, there are 2 functions that MAYBE I can use:

Using the setOnEditorActionListener:

private EditText valor;    

protected void onCreate(Bundle savedInstanceState) {

     valor = findViewById(R.id.valor);

     valor.setOnEditorActionListener(new TextView.OnEditorActionListener() {
           @Override
           public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
               if(digitou algo){
                  System.out.println("DIGITOU!");
               }
               return false;
           }
    });

}

Using the setOnKeyListener:

valor.setOnKeyListener(new View.OnKeyListener() {
         @Override
         public boolean onKey(View view, int i, KeyEvent keyEvent) {
               if(digitou algo){
                  System.out.println("DIGITOU!");
               }
               return false;
         }
 });

It is possible to do this?

1 answer

2


You can use the Textwatcher:

valor.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        System.out.println("DIGITOU!");
    }
});
  • That’s exactly what I needed. Thank you. More specifically onTextChanged because each character pressed it will do the same action. Hug!

Browser other questions tagged

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