Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an EditText was changed or not?

Tags:

I need to know if an EditText was changed or not, not whether or not the user inputted some text in the field, but only the if String was changed.

like image 229
Eugene Avatar asked Sep 12 '11 17:09

Eugene


People also ask

How do I know if EditText has changed?

Implement a TextWatcher. It gives you three methods, beforeTextChanged , onTextChanged , and afterTextChanged . The last method shouldn't be called until something changes anyway, so that's a good thing to use for it.

How do I show errors in EditText?

In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id. username); til.

How do I use addTextChangedListener?

While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input. EditText uses TextWatcher interface to watch change made over EditText. For doing this, EditText calls the addTextChangedListener() method.


2 Answers

You need a TextWatcher

See it here in action:

EditText text = (EditText) findViewById(R.id.YOUR_ID); text.addTextChangedListener(textWatcher);   private TextWatcher textWatcher = new TextWatcher() {    public void afterTextChanged(Editable s) {   }    public void beforeTextChanged(CharSequence s, int start, int count, int after) {   }    public void onTextChanged(CharSequence s, int start, int before,           int count) {    } } 
like image 110
j7nn7k Avatar answered Sep 17 '22 13:09

j7nn7k


If you change your mind to listen to the keystrokes you can use OnKeyListener

    EditText et = (EditText) findViewById(R.id.search_box);       et.setOnKeyListener(new View.OnKeyListener() {          @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {             //key listening stuff             return false;         }     }); 

But Johe's answer is what you need.

like image 27
Y.A.P. Avatar answered Sep 21 '22 13:09

Y.A.P.