开发者

Why it is not possible to put the two TextWatcher in One activity for two different edittext that change the value with respect of each other?

开发者 https://www.devze.com 2023-04-12 07:20 出处:网络
In My Application there is two EditText and one TextView. I have set the Two Textwatcher on that both EditText. Based on the Value in that EditText, The Value of another EditText is changed and it als

In My Application there is two EditText and one TextView. I have set the Two Textwatcher on that both EditText. Based on the Value in that EditText, The Value of another EditText is changed and it also apply to textView. All Goes Nice. But if i put the that Another Edittext as like the value of it to be change based on the Another EditText then it gives me stackOverflow Error. I dont know Where i am wrong. And I am not able to Put the TextWatcher for both the EditText to change the value with respect to each other.

Please Help me for it. Thanks. Code :

 amountText.setText(""); // TextView
    excludedText.setText(""); // EditText
    includedText.setText(""); // EditText


    includedText.addTextChangedListener(new TextWatcher() {

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

                if(!(includedText.getText().toString().equals("")))
                {
                    double included = Double.parseDouble(includedText.getText().toString());
                    included = roundTwoDecimals(included);
        //          double amountValue = (excluded*15/100);

                    String amt = String.valueOf(roundTwoDecimals(included-(included/1.15)));
                    String excluded = String.valueOf(included/1.15);
                    System.out.println("The Amount is: "+amt);
                    amountText.setText(amt);
                    try{
                        excludedText.setText("");
                        excludedText.setText(excluded); //////// Error Line
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    amountText.setText("");
                    excludedText.setText(""); // Error line
                }

        }

        @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

        }
    });

    excludedText.addTextChangedListener(new TextWatcher() 
    {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!(excludedText.getText().toString().equals("")))
            {
                double excluded = Double.parseDouble(excludedText.getText().toString());
                excluded = roundTwoDecimals(excluded);

                String amt = String.valueOf(roundTwoDecimals(excluded*0.15));
                String included = String.valueOf(roundTwoDecimals(excluded+(excluded*0.15)));
                System.out.println("The Amount is: "+amt);
                amountText.setText(amt);
                includedText.setText(included);
            }
            else
            {
                amountText.setText("");
                includedText.setText("");
            }

        }

        @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

        }
    });

Here my Logcat is:

10-11 09:47:28.288: ERROR/AndroidRuntime(363): FATAL EXCEPTION: main
10-11 09:47:28.288: ERROR/AndroidRuntime(363): java.lang.StackOverflowError
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at     android.text.DynamicLayout.reflow(DynamicLayout.java:261)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at  android.widget.TextView.makeNewLayout(TextView.java:4851)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.checkForRelayout(TextView.java:5348)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2688)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:73)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.ja开发者_运维知识库va:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:73)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:73)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:73)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:73)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2556)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.EditText.setText(EditText.java:75)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2531)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextView.setText(TextView.java:2691)
10-11 09:47:28.288: ERROR/AndroidRuntime(363):     at android.widget.TextVie

Please refer this logcat and help me to solve it. Thanks.


This is a problem of stackoverflow you are going to infinite loop.

You are updating value of one edittext from second one and vice-verse .

For example ::

When you enter character in first edit text it will set text of second edit text and at that time in second edit text you have write a code to set text of first edit text so that you are going to infinite loop.

EDIT

et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);

OnKeyListener onKeyListener = new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                String str = ((EditText)v).getText().toString();
                if(v == et1) {
                    et2.setText(str);
                }else {
                    et1.setText(str);
                }
                return false;
            }
        };
        et1.setOnKeyListener(onKeyListener);
        et2.setOnKeyListener(onKeyListener);


You could try to put the code into "afterTextChanged", but I think it will result in the same infinite loop. That's why I don't understand how both inputs can depend on each other, meaning, if EditText-A changes the text of ET-B, then how can ET-B change the text in ET-A again, that doesn't make any sense.


Everytime a text is changed in an EditText the TextWatcher is invoked (if it is defined and depending on which overridden methods you implemented). So based on what you want to accomplish, if EditText A's text is edited then the TextWatcher is invoked and edits EditText B's text which then invokes its corresponding TextWatcher which edits EditText A's text which invokes... in short it is a bad practice.

0

精彩评论

暂无评论...
验证码 换一张
取 消