0

I'm new to Android development and I want to create a method that runs every time my editText is changed.

In swift I use this function:

func fieldChanged(textfieldChange: UITextField){

}

I can't find anywhere a similar function for Java and Android Studio, are there any suggestions?

Serge K.
  • 5,303
  • 1
  • 20
  • 27
Alex Karapanos
  • 895
  • 2
  • 7
  • 20
  • 4
    Possible duplicate of [android edittext onchange listener](https://stackoverflow.com/questions/11134144/android-edittext-onchange-listener) – Falco Winkler Jul 21 '17 at 07:54
  • Why is javascript as tag? Do you ask for Java/Kotlin or are you using javascript and Ionic, Electron or similar...? – sharp Jul 21 '17 at 08:07

2 Answers2

1
// Globally declare a variable

private TextWatcher mTextWatcher;

// Call textChanger() method in onCreate

private void textChanger() {
        mTextWatcher = new TextWatcher() {
            @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.length() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
                else
                    mEditText.setError("enter correct");
            }

            @Override
            public void afterTextChanged(Editable s) { }
        };
        mEditText.addTextChangedListener(mTextWatcher);
    }
Athar Iqbal
  • 458
  • 3
  • 12
0

try this

      editText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

                        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163