Wednesday, January 22, 2014

Listen for text changes on EditText

Assume you want to enable a button only if some changes have been made to a form. You can do this by using android.text.TextWatcher
Simply create a new instance of TextWatcher and implement the required methods
There you have it. For more info checkout http://developer.android.com/reference/android/text/TextWatcher.html

import android.text.TextWatcher;
private void addEditTextChangeListeners() {
TextWatcher textListener = new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {}
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {}
public void afterTextChanged(Editable editable) {
saveButton.setEnabled(editable.length() > 0);
}
};
url.addTextChangedListener(textListener);
}
view raw gistfile1.java hosted with ❤ by GitHub

No comments:

Post a Comment