简体   繁体   English

尝试在每次检测到某个字符串时清除TextWatcher并继续读取EditText

[英]Trying to clear a TextWatcher every time it detects a certain string and continue reading the EditText

I have an EditText in which I want to be able to detect when the user types in a flagged word. 我有一个EditText,我希望在其中可以检测到用户何时输入标记的单词。 When this word is detected, a button becomes visible and the user has more options. 当检测到该单词时,按钮变为可见,并且用户具有更多选项。 I have been able to do this with a TextWatcher. 我已经能够使用TextWatcher做到这一点。 The problem is that I'm storing the flagged word that is used in an array every time it is detected by the Textwatcher. 问题是,每次TextTexter检测到标记的单词时,我都会存储在数组中使用的标记的单词。 But once the word is in the EditText, every character that is entered afterward causes the flagged word to be entered into the array again since the flagged word is still in the EditText. 但是,一旦单词在EditText中,则以后输入的每个字符都会导致将已标记的单词再次输入到数组中,因为标记的单词仍在EditText中。 I was wondering, is there any way to clear the TextWatcher so that once I find a flagged word, it starts watching only for what I type after that? 我想知道,是否有任何方法可以清除TextWatcher,以便一旦我找到一个标记的单词,它便开始监视我随后输入的内容? The word variable in the code below is where the flagged word the program is checking for is stored. 以下代码中的word变量存储了程序正在检查的标记单词。

    txtMessage.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            // TODO Auto-generated method stub
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            String tmp = s.toString().trim().toLowerCase();
            if (flagged == true) {
                CheckWords.setVisibility(0);
            }
            if (tmp.contains(word.toLowerCase())) {
                flagwordsused[i] = word;
                //txtPhoneNo.setText(""+tmp);

                i++;
                flagged = true;
            }

        }
    });

Any help you could give me would be appreciated. 您能给我的任何帮助将不胜感激。 Maybe there is some completely different way of accomplishing this task. 也许有完全不同的方式来完成此任务。

I got it. 我知道了。 I just need to break the tmp string into substrings and see if the flag word is contained in one of the substrings instead of the entire EditText. 我只需要将tmp字符串分成子字符串,然后查看标志字是否包含在子字符串之一中而不是整个EditText中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM