EditText-onChange-events

simple sample

edtext.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) {
      // setEnable or disabled for ok-button
   }
});

simple sample


complete sample

private Pattern namePattern = Pattern.compile("^([A-Z][a-z]{1,29})[,][ ]([A-Z][a-z]{1,29})$");
private boolean isNameOk=true;


private void setEnabledbnOk() {
    bnSaveChanges.setEnabled(this.isNameOk);
}

// in onCreate
edtext.addTextChangedListener(new TextWatcher() {
   // check, if
   private boolean isValid(CharSequence s) {
       return namePattern.matcher(s).matches();
   }

   @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) {
      if (isValid(s))  {
          tvValidStdname.setText("");  // a textview which has a blank or a star (*)
          isNameOk = true;
      }
      else {
          tvValidStdname.setText("*");
          isNameOk=false;
      }
      setEnabledbnOk();
   }
});