Executing an action after each character typed in an EditText

0

It's the following, I'm not able to find out if the user typed something in EditorText . What I want is that, every character, number or letter that the user types, I can do an action right after.

I researched a lot, I did several tests but I could not because I was new to 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;
         }
 });

Is it possible to do this?

    
asked by anonymous 24.02.2018 / 01:02

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!");
    }
});
    
24.02.2018 / 10:17