gpt4 book ai didi

android - 如何删除 EditText TextChangedListener 中的递归?

转载 作者:行者123 更新时间:2023-11-29 17:54:09 26 4
gpt4 key购买 nike

假设我想从编辑文本中删除一些不好的词。父亲叔叔表哥 King 会被 F**K... 但问题是,如果我更改文本,它会再次更改处理程序。任何人都知道解决方案。请帮忙。代码在那里。

search_name.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
search_name.setText(s.toString().replace("something", "s**ething"));
// here comes it repeatedly
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});

最佳答案

我同意@njzk2 re:InputFilter 的评论,如果递归是你想解决的问题,你可以尝试这样的事情来防止它:

// make your TextWatcher a class variable
protected TextWatcher mTextWatcher = new TextWatcher()
{
public void afterTextChanged(Editable s) {
textFixerUpper(search_name, s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};


protected textFixerUpper(EditText t, String s)
{
t.removeTextChangedListener(mTextWatcher); // remove the listener
t.setText(s.replace("something", "s**ething")); // update the text
t.addTextChangedListener(mTextWatcher); // add the listener back
}

或者通过设置一个 bool 标志而不是删除并重新添加监听器....尽管两者看起来有点困惑。

关于android - 如何删除 EditText TextChangedListener 中的递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com