Методы TextWatcher, вызываемые несколько раз - PullRequest
0 голосов
/ 18 сентября 2018

У меня есть один текст для редактирования в адаптере, я прикрепил к нему addTextChangedListener, и все его методы вызывались несколько раз, мой вывод похож

1 Ответ

0 голосов
/ 18 сентября 2018

РЕДАКТИРОВАТЬ Использование таймера для отмены обработки, когда пользователь печатает

Вы можете попытаться добавить таймер для обработки текста и отменить эту обработку, пока пользователь печатает.

private Timer timer;

myButton.addTextChangedListener(new TextWatcher() {  
    @Override
    public void afterTextChanged(Editable arg0) {
        // user typed: start the timer
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // do your actual work here
            }
        }, 600); // 600ms delay before the timer executes the „run“ method from TimerTask
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // nothing to do here
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // user is typing: reset already started timer (if existing)
        if (timer != null) {
            timer.cancel();
        }
    }
});

Надеюсь, это поможет!

...