Events of TextWatcher are being called twice

好久不见. 提交于 2019-11-29 17:44:35

问题


On my application I put TextWatcher on EditText. When I change the text of the EditText, the events of TextWatcher are being called twice.

I am using emulator for running the app.


回答1:


How does your code looks like? that is the normal behaviour of TextWatcher. Example:

myInput.addTextChangedListener(new TextWatcher() {
        boolean mToggle = false;

        public void onTextChanged(CharSequence cs, int s, int b, int c) {}

        public void afterTextChanged(Editable editable) {
            if (mToggle) { 
                Toast.makeText(getBaseContext(), "HIT KEY",Toast.LENGTH_LONG).show();
            }
            mToggle = !mToggle;
        }

        public void beforeTextChanged(CharSequence cs, int i, int j, int k) {}
    });



回答2:


My problem was I added the textWatcher twice mEditText.addTextChangedListener(mTextWatcher), which leads to calling its callbacks twice!

I had added the textWatcher once in onCreate() and once in onStart(). I should only add in onStart and remove in onStop().



来源:https://stackoverflow.com/questions/12073998/events-of-textwatcher-are-being-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!