开发者

Unable to put the Two TextWatcher Listener for two Different EditText?

开发者 https://www.devze.com 2023-04-11 19:52 出处:网络
I have two EditText and one TextView in Layout. I want to change the Value of EditText and TextView with respect to another EditText Value. And Same for the another editText. I have Implemented the co

I have two EditText and one TextView in Layout. I want to change the Value of EditText and TextView with respect to another EditText Value. And Same for the another editText. I have Implemented the code like below.

 amountText.setText("");
    excludedText.setText("");
    includedText.setText("");


    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
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    amountText.setText("");
                    //excludedText.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

        }
    });

    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);
        //      double amountValue = (excluded*15/100);

                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

        }
    });

But while i run the application, i got the error like stackoverflow.

error Logcat is:

10-08 10:46:19.149: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main
10-08 10:46:19.149: ERROR/AndroidRuntime(695): java.lang.StackOverflowError
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.makeNewLayout(TextView.java:4851)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.checkForRelayout(TextView.java:5348)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2688)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-08 10:46:19.149: E开发者_如何学CRROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2556)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.EditText.setText(EditText.java:75)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2531)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextView.setText(TextView.java:2691)
10-08 10:46:19.149: ERROR/AndroidRuntime(695):     at android.widget.TextVie

I dont know where i am wrong. .

Please help me to implement such method. Thanks.


try to decrease your execution of code multiple types say like this

your are doing

 if(!(includedText.getText().toString().equals("")))
                {
                    double included = Double.parseDouble(includedText.getText().toString());

...

which can be done by this also

String stringIncluded=includedText.getText().toString();
if(!(stringIncluded.equals("")))
                    {
                        double included = Double.parseDouble(stringIncluded);

what i mean to say try to remove such code which is common so that execution process will be compact and may leads to resolve your issue

0

精彩评论

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