Android 编辑文本删除文本更改列表器

Android 编辑文本删除文本更改列表器,android,android-edittext,addtextchangedlistener,Android,Android Edittext,Addtextchangedlistener,我一直在寻找如何删除文本更改侦听器的答案 这是我目前的代码: public void enableTextChangedListener(boolean enableFormatting){ if (enableFormatting) { if (!"1".equals(mAmountEditText.getTag())) { mAmountEditText.addTextChangedListener(new Strin

我一直在寻找如何删除文本更改侦听器的答案

这是我目前的代码:

 public void enableTextChangedListener(boolean enableFormatting){
        if (enableFormatting) {
            if (!"1".equals(mAmountEditText.getTag())) {
                mAmountEditText.addTextChangedListener(new StringUtils.NumberTextWatcherForThousand(mAmountEditText));
                mAmountEditText.setTag("1");

  }
        }
        else {
            mAmountEditText.removeTextChangedListener(new StringUtils.NumberTextWatcherForThousand(mAmountEditText));
        }
    }
当我的boolean enableFormatting为False时,textchangelistener仍然存在


如果您想更清楚地解释我的代码,我可以提供类号rtextwatcherforthousand。

您需要保留对侦听器的引用,现在您在尝试删除旧侦听器时正在创建一个新的侦听器。 大概是这样的:

textWatcher = new StringUtils.NumberTextWatcherForThousand(mAmountEditText);
public void enableTextChangedListener(boolean enableFormatting){
    if (enableFormatting) {
        if (!"1".equals(mAmountEditText.getTag())) {
            mAmountEditText.addTextChangedListener(textWatcher);
            mAmountEditText.setTag("1");

}
    }
    else {
        mAmountEditText.removeTextChangedListener(textWatcher);
    }
}

每次调用
enableTextChangedListener()
时,您都在创建一个新的
NumberTextWatcherForThousand
。当您使用新的、未使用的
NumberTextWatcherForThousand
调用
removeTextChangedListener()
时,它将找不到要删除的对象,因此实际上不会发生任何事情。您需要保留对
NumberTextWatcherForThousand
的引用,并传入
addTextChangedListener()
调用。我该怎么做?很抱歉,我不知道@MikeM。我试过这个,它说:textWatcher=new StringUtils.numberTextWatcherOrthousand(mAmountEditText),其中mAmountEditText始终为空。反正我用的是黄油刀。