0

I have an app in which i have lots of edit text but in some edit text i only want to implement only one method of TextWatcher. How do i do that kindly guide.

code:-

private TextWatcher m_textWatcher = 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) {
        isFieldsEmpty();
    }

    @Override
    public void afterTextChanged(Editable s) {
        m_mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = m_mainLayout.getRootView().getHeight() - m_mainLayout.getHeight();
                if (!(heightDiff > RewardUtil.dpToPx(mContext, 200))) { // if more than 200 dp, it's probably a keyboard...
                    m_otpEditText.clearFocus();
                }
            }
        });
    }
};
 m_otpEditText.addTextChangedListener(m_textWatcher);
Suraj Makhija
  • 1,376
  • 8
  • 16
Raghav
  • 137
  • 5
  • 11

3 Answers3

2

Create metod lke this.

   public static class MyTextWatcher implements TextWatcher {

        private EditText mEditText;

        public MyTextWatcher(EditText editText) {
            mEditText = editText;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            oldText = mEditText.toString();
        }
        ....
    }

And use like this...

  m_otpEditText.addTextChangedListener(new MyTextWatcher(mFirstEditText));

It will help you. Thanks.

Kush
  • 1,080
  • 11
  • 15
2

First of all, you can create a class as follows :

public class CustomTextWatcher implements 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) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

Thereafter, just override the method/s you need. Suppose you need only onTextChanged method, then you can do as follows :

m_otpEditText.addTextChangedListener(new CustomTextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
        });
Suraj Makhija
  • 1,376
  • 8
  • 16
0

TextWatcher class gives you flexibility to override methods whichever you need. If you really need only one method then override only one, but you have to always implement all methods .

Mansuu....
  • 1,206
  • 14
  • 27