How to detect only user's input in EditText?

374 views Asked by At

I have EditText and TextWatcher that listen this view. Inside TextWatcher I format specific input strings. It calls my TextWatcher again, but I don't want it. I want to detect only user input. So, I tried a lot of methods to reach it, but they didn't have any effect. User can input text very fast and I can't remove listener while format text because I loose any events. Maybe android framework has other mechanisms to listen EditText?

Code. Now this code doesn't work. We can remove listener before formatting and add after or use some flag, but it doesn't work for me.

    public static class LoginWatcher implements TextWatcher {

    private OnLoginEnterListener listener;
    private LoginFilter loginFilter = new LoginFilter();
    private EditText target;

    public LoginWatcher(OnLoginEnterListener onLoginInputListener) {
        listener = onLoginInputListener;
        target = listener.getTarget();
    }

    @Override
    public synchronized void afterTextChanged(Editable s) {

        boolean isSymbolsChecked = loginFilter.check(s.toString());
        boolean isEmail = StringUtils.isEmailValid(s.toString());
        boolean isPhoneNumber = isPhoneNumber(s.toString());

        if ((isSymbolsChecked && isEmail) || isPhoneNumber) {
            listener.onCorrectLoginEntered(s.toString());
            listener.onCheckedSymbolsEntered(s.toString());
        } else {
            listener.onIncorrectLoginEntered(s.toString());
            if (isSymbolsChecked) {
                listener.onCheckedSymbolsEntered(s.toString());
            } else {
                listener.onUnsupportedSymbolsEntered(s.toString());
            }
        }

        if (isPhoneNumber) {
            String formatted = formatPhoneNumber(s.toString());
            target.setText(formatted);
            target.setSelection(formatted.length());
        } else {
            String unformatted = unFormatPhoneNumber(s.toString());
            target.setText(unformatted);
            target.setSelection(unformatted.length());
        }
    }
}
1

There are 1 answers

0
Exensor On

Have you tried using onUserInteractionListener method? You can try activating a flag in the Activity if the user has interacted using something like this:

@Override
 public void onUserInteraction () {
     super.onUserInteraction ();
     userIsInteracting = true;
 }