Vivekanand
Vivekanand

Reputation: 755

android- how to implement text watcher, example

i have a program where i have multiple editText and one final editText.... I want to implement TextWatcher to reflect the changes in the total edittext.. But I don't know how to implement all this for changes in multiple editTexts.

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

public void onTextChanged(CharSequence s, int start, int before, int count) {               
    total+=new Integer(s.toString());
    _EDTotal.setText(""+total);
}

in the above example I will face a problem... When i enter the value at the first I will setText the correct value in the _EDTotal, but if i delete (use backSpace) in the editText using the listener I will not be able to replace it with new prob.

Upvotes: 1

Views: 1805

Answers (1)

NrNazifi
NrNazifi

Reputation: 1651

try like this:

edittext.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) {
       //your action
   }
});

Upvotes: 1

Related Questions