Skip to content
dexafree edited this page Aug 21, 2014 · 1 revision

This annotation will trigger an event when an EditText's text is changed.

##Annotation arguments @OnTextChanged uses an extra "method" argument to specify the listener method which will call this method.

The annotation must be:

@OnTextChanged(value=R.id.edit_text, method=OnTextChanged.Method.BEFORE_TEXT_CHANGED)
// or
@OnTextChanged(value=R.id.edit_text, method=OnTextChanged.Method.ON_TEXT_CHANGED)
// or
@OnTextChanged(value=R.id.edit_text, method=OnTextChanged.Method.AFTER_TEXT_CHANGED)

How to use

The method should be this way (the method and variable names can be changed):

BeforeTextChanged

  • public void beforeTextChanged(CharSequence sequence)
  • public void beforeTextChanged(TextView textView, CharSequence sequence)
  • public void beforeTextChanged(CharSequence sequence, int start, int count, int after)
  • public void beforeTextChanged(TextView textView, CharSequence sequence, int start, int count, int after)

OnTextChanged

  • public void onTextChanged(CharSequence sequence)
  • public void onTextChanged(TextView textView, CharSequence sequence)
  • public void onTextChanged(CharSequence sequence, int start, int before, int count)
  • public void onTextChanged(TextView textView, CharSequence sequence, int start, int before, int count)

AfterTextChanged

  • public void afterTextChanged(Editable editable)

Example

@OnTextChanged(value=R.id.edit_text, method=OnTextChanged.Method.ON_TEXT_CHANGED)
public void onTextChanged(CharSequence sequence) {
    Toast.makeText(this, "Sentence written: $sequence", Toast.LENGTH_SHORT).show();
}