TextWatcher 事件被多次触发 [英] TextWatcher events are being fired multiple times

查看:51
本文介绍了TextWatcher 事件被多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TextWatcher 有一个烦人的问题.我一直在网上搜索,但找不到任何东西.如果有人可以帮助我,不胜感激.

I have an annoying problem with TextWatcher. i've been searching the web but couldnt find anything. appreciate if someone could assist me.

由于某种原因,文本更改时对 TextWatcher 事件的调用不稳定.有时它们会被触发一次(就像它们应该的那样),有时是两次,有时是 3 次.不知道为什么,整个事情很简单.有时 afterTextChanged() 上的 Editable 参数也会在 toString() 和 length() 中返回空值.

For some reason the calls to the TextWatcher events upon one text change are erratic. sometimes they are being triggered once (like they should be), sometimes twice, and sometimes 3 times. have no idea why, the whole thing is very straight forward. also sometimes the Editable parameter on afterTextChanged() returns empty values in toString() and length().

代码如下:

    private TextWatcher mSearchAddressTextChangeListener = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

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

        @Override
        public void afterTextChanged(Editable searchedAddress) {
           System.out.println("called multiple times.");   
        }
    };

afterTextChanged()(和AsyncTask)内部,我没有对文本或EditText 视图进行任何更改.

inside afterTextChanged() (and the AsyncTask) im not making any change to the text or the EditText view.

我在TextWatcher的事件被调用两次中看到了这个问题,但我的事件触发次数多于(或少于)两次.

i saw the question asked in Events of TextWatcher are being called twice, but im having the events triggered more (or less) than twice.

无论如何,感谢您的帮助.

anyway, appreciate any help.

我删除了 afterTextChanged() 的内容,因为即使没有我的代码也会发生这个问题.是什么让我相信这是一个错误.当在常规字符之后立即输入空格"字符(事件处理程序被触发两次)或在删除常规字符之后的空格"字符时(退格.事件处理程序被触发 3 次),就会发生该错误.帮助将不胜感激.

I removed the content of afterTextChanged() cause this problem is happening even without my code. what leads me to believe this is a bug. The bug is occuring when a 'space' char is entered right after a regular char (event handlers are triggered twice) or when a 'space' char after a regular char is removed (backspace. event handlers are triggered 3 times). help will still be appreciated.

推荐答案

我遇到了同样的问题,当我在连续文本的末尾用光标按退格键时,afterTextChange 被调用了 3 次:- 第一次使用正确的 s 值- 第二次有明确的价值- 第三次再次使用正确的值

I had the same kind of problem, when I pressed backspace with cursor at the end of a continuous text, afterTextChange was called 3 times: - The first time with the correct s value - The second time with a clear value - The third time with the correct value again

在网上搜索了很多之后,我尝试将我的 EditText inputType 更改为

After having search a lot on the web, I tried to change my EditText inputType to

android:inputType="textNoSuggestions"

不要问我为什么,但它有效,afterTextChanged 现在只调用一次.

Don't ask me why, but it worked, afterTextChanged is now called only once.

这篇关于TextWatcher 事件被多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆