Search code examples
androidandroid-edittextandroid-keypad

How to capture event when user has entered a new character in a EditText?


I Have a EditText restricted to numeric characters, and i need to perform special actions when user enters numbers and when user press "." character on virtual keyboard.

numberEditText = new EditText(this);
numberEditText.setInputType(InputType.TYPE_CLASS_NUMBER);

I know that exists this listener:

numberEditText.setOnEditorActionListener(new OnEditorActionListener() {         
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {               
                return false;
            }
        });

But i don't know how to use it to achieve my needs.

Any help will be grated.

Thanks


Solution

  • This is how -

    editText.addTextChangedListener(new TextWatcher() {
    
           @Override
           public void afterTextChanged(Editable s) {}
    
           @Override    
           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }
    
           @Override    
           public void onTextChanged(CharSequence s, int start,
             int before, int count) {
              if(s.equals("."))
                editText.setText("");
           }
          });