删除 addTextChangedListener() 当 EditText 值以编程方式更改而不是手动更改时

Remove addTextChangedListener() When the EditText values changed programmatically not manually

我在一个网站上工作,我正在使用 Javascript 更改文本。

  1. When user change the quantity I am changing the total price & sale price value.
  2. When user change the sale price I am changing the discount value.
  3. When user change the discount I am changing the sale price value.

现在我想要在我的 android 应用程序中做同样的事情。

我在每个 EditText.

上添加了文本更改事件监听器

喜欢

数量EditText

    holder.inputSaleQuantity.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

折扣编辑文本。

holder.inputSaleDiscount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.d("SocialCodia", "afterTextChanged: inputSaleDiscount Event Listener Called");
                int input = 0;
                if (editable.toString().trim().length() > 0) {
                    input = Integer.parseInt(editable.toString().trim());
                }
                    discountInputEvent(holder, input);
            }
        });

待售价格 EditText。

holder.inputSalePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                priceEvent(holder);
                Log.d("SocialCodia", "afterTextChanged: inputSalePrice Event Listener Called");
            }
        });

POSSIBILITIES WHY EXECUTING METHOD AGAIN AND AGAIN.

When I am changing the any EditText's value like, Changing the Quantities EditText's value, Its calling sale price edit text change listener as well.

Because on change of Quantity value I am changing the sell price value. So when the sell price value change there text change listener executing.

我正在调用的方法。

private void priceEvent(ViewHolder holder) {
        int totalPrice = Integer.parseInt(holder.inputTotalPrice.getText().toString().trim());
        int sellPrice = Integer.parseInt(holder.inputSalePrice.getText().toString().trim());
        int discount = percentage(sellPrice, totalPrice);
        holder.inputSaleDiscount.setText(String.valueOf(discount));
    }

    private void discountInputEvent(ViewHolder holder, int input) {
        int totalPrice = Integer.parseInt(holder.inputTotalPrice.getText().toString().trim());
        int price = percentageDec(totalPrice, input);
        holder.inputSalePrice.setText(String.valueOf(price));
    }

    private int percentage(int partialValue, int totalValue) {
        Double partial = (double) partialValue;
        Double total = (double) totalValue;
        Double per = (100 * partial) / total;
        Double p = 100 - per;
        return p.intValue();
    }

    private int percentageDec(int totalValue, int per) {
        if (per == 0 || String.valueOf(per).length() < 0)
            return totalValue;
        else {
            Double total = (double) totalValue;
            Double perc = (double) per;
            Double price = (total - ((perc / 100) * total));
            Integer p = price.intValue();
            return p;
        }
    }

简而言之明确问题:

我只想用addTextChangeEventListener()EditText的值手动更改。 但是当我使用脚本更改值时,我不想使用 addTextChangeEventListner().

我怎样才能做到这一点。或者我该如何解决这个问题。

-谢谢

简答:

TextWatcher's 需要非匿名定义(即使用变量),以便您可以使用 addTextChangedListener(myWatcher) 注册它们,并且无论何时您想要停止它们,您都可以取消注册它们使用 removeTextChangedListener(myWatcher) 然后在完成脚本修改后使用 addTextChangedListener(myWatcher) 重新注册它们。

长答案

构建初始 TextWatcher 变量并将它们注册到 EditTexts

// Build variables for text watchers

TextWatcher quantityWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

TextWatcher salePriceWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

TextWatcher discountWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

首先注册所有的文本观察者。

registerTextWatchers();

private void registerTextWatchers() {
    holder.inputSaleQuantity.addTextChangedListener(quantityWatcher);
    holder.inputSalePrice.addTextChangedListener(salePriceWatcher);
    holder.inputSaleDiscount.addTextChangedListener(discountWatcher);   
}

每当您以编程方式更改 EditText 时取消注册观察者,并在完成后重新注册它们

private void unregisterTextWatchers() {
    holder.inputSaleQuantity.removeTextChangedListener(quantityWatcher);
    holder.inputSalePrice.removeTextChangedListener(salePriceWatcher);
    holder.inputSaleDiscount.removeTextChangedListener(discountWatcher);    
}

private void priceEvent(ViewHolder holder) {
    //...
    
    unregisterTextWatchers();
    holder.inputSaleDiscount.setText(String.valueOf(discount));
    registerTextWatchers();
}

private void discountInputEvent(ViewHolder holder, int input) {
    //...
    
    unregisterTextWatchers();
    holder.inputSalePrice.setText(String.valueOf(price));
    registerTextWatchers();
}

每当您以编程方式更改 EditText 值时都重复相同的操作

您可以尝试这个解决方案,首先您应该为每个编辑文本定义一个文本观察器,如下所示:

private TextWatcher quantityWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

然后将其添加到编辑文本中,如下所示:

holder.inputSaleQuantity.addTextChangedListener(quantityWatcher);

因此,当您以编程方式设置文本时,只需从编辑文本中删除观察者,设置文本后您可以像这样再次设置它:

private void priceEvent(ViewHolder holder) {
        holder.inputSaleQuantity.removeTextChangedListener(quantityWatcher);

        int totalPrice = Integer.parseInt(holder.inputTotalPrice.getText().toString().trim());
        int sellPrice = Integer.parseInt(holder.inputSalePrice.getText().toString().trim());
        int discount = percentage(sellPrice, totalPrice);
        holder.inputSaleDiscount.setText(String.valueOf(discount));

        holder.inputSaleQuantity.addTextChangedListener(quantityWatcher);

    }