我有一个 EditText,我希望能够检测用户何时输入标记的单词。当检测到这个词时,按钮就会变得可见,并且用户有更多选项。我已经能够使用 TextWatcher 来做到这一点。问题是,每次文本观察器检测到它时,我都会将标记的单词存储在数组中。但是,一旦该单词位于 EditText 中,之后输入的每个字符都会导致标记的单词再次输入到数组中,因为标记的单词仍在 EditText 中。我想知道,有什么方法可以清除 TextWatcher,以便一旦我找到标记的单词,它就开始只监视我之后输入的内容?下面代码中的 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;
}
}
});
如果您能给我任何帮助,我将不胜感激。也许有一些完全不同的方法来完成这个任务。
最佳答案
我明白了。我只需要将 tmp 字符串分解为子字符串,然后查看标志词是否包含在其中一个子字符串中,而不是整个 EditText 中。
关于java - 每次检测到某个字符串时尝试清除 TextWatcher 并继续读取 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10108702/