如何使用两个EditTexts防止无限循环?

时间:2016-06-20 16:46:48

标签: android

我有两个TextView EditText并在TextChangedListener上分配了每个TextView,它们相互引用,更改了另一个textview的文本。具体做法是:

rateText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!rateText.getText().toString().equals("")) {
                float price = Float.parseFloat(priceText.getText().toString());
                float rate = Float.parseFloat(rateText.getText().toString());

                // REFERNCES THE OTHER TEXT
                amountText.setText(Float.toString(price / rate));
            } else {
                amountText.setText("");
            }
        }
    });
    amountText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!rateText.getText().toString().equals("")) {
                float price = Float.parseFloat(priceText.getText().toString());
                float amount = Float.parseFloat(amountText.getText().toString());

                // REFERNCES THE OTHER TEXT
                rateText.setText(Float.toString(price / amount));
            } else {
                rateText.setText("");
            }
        }
    });

这会导致无限循环,因为每个侦听器都会触发另一个侦听器。如何在不需要按钮或其他东西的情况下阻止这种情况?

2 个答案:

答案 0 :(得分:0)

我同意@ Lotharyx的建议,首先尝试使用布尔值。如果在与添加这些TextWatchers的方法相同的范围内声明,则boolean变量应该只是final,否则您可以使用实例变量(不必是静态的)。

您可以尝试的另一件事是在afterTextChanged()中的代码体之前删除TextWatcher,并在另一个EditText上调用setText()之后再次添加它。但这也需要您创建TextWatcher对象实例变量或将它们声明为final。

    @Override
    public void afterTextChanged(Editable s) {
        rateText.removeTextChangedListener(rateTextListener);

        if(!rateText.getText().toString().equals("")) {
            float price = Float.parseFloat(priceText.getText().toString());
            float amount = Float.parseFloat(amountText.getText().toString());

            // REFERNCES THE OTHER TEXT
            rateText.setText(Float.toString(price / amount));
        } else {
            rateText.setText("");
        }

        rateText.addTextChangedListener(rateTextListener);
    }

答案 1 :(得分:0)

你可以在字段中保存TextWatchers并使用静态方法“静默地”设置文本而不通知听众这样:

private TextWatcher rateWatcher; //hold watchers in fields
private TextWatcher amountWatcher;  

//...  
void yourMethodOrCallback() { //whatever
//...
    amountWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!rateText.getText().toString().equals("")) {
                float price = Float.parseFloat(priceText.getText().toString());
                float amount = Float.parseFloat(amountText.getText().toString());

                // REFERNCES THE OTHER TEXT
                setTextSilently(amountText, amountWatcher, Float.toString(price / amount));

            } else {
                setTextSilently(rateText, rateWatcher, "");
            }
        }
    };
    rateWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!rateText.getText().toString().equals("")) {
                float price = Float.parseFloat(priceText.getText().toString());
                float rate = Float.parseFloat(rateText.getText().toString());

                // REFERNCES THE OTHER TEXT
                setTextSilently(amountText, amountWatcher, Float.toString(price / rate));
            } else {
                setTextSilently(amountText, amountWatcher, "");
            }
        }
    };
    rateText.addTextChangedListener(rateWatcher);
    amountText.addTextChangedListener(amountWatcher);

}

private static void setTextSilently(EditText editText, TextWatcher textWatcher, CharSequence text) {
    editText.removeTextChangedListener(textWatcher); //removing watcher temporarily
    editText.setText(text); //setting text
    editText.addTextChangedListener(textWatcher); //readding watcher 
}