add a listener to editText

Asked

Viewed 168 times

-2

how do I make an action be executed, every time the user type some letter in editText, I searched but did not find what wanted, if someone knows at least a hint of how to search already help

1 answer

1


In your Edittext you add the method addTextChangedListener() as follows:

package genesysgeneration.svg;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        EditText editText = (EditText)findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
                //aqui você executa uma determinada ação antes da modificação do editText
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                //aqui você executa uma determinada ação durante a modificação do editText

            }

            @Override
            public void afterTextChanged(Editable s) {

                //aqui você executa uma determinada ação depois da modificação do editText

            }
        });

    }
}

In it you have 3 options for executing an action, before edtText is modified, during and after. You choose one of the three according to your need.

Browser other questions tagged

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