Невозможно поместить прослушиватель Two TextWatcher для двух разных EditText? - PullRequest
0 голосов
/ 08 октября 2011

У меня есть два EditText и один TextView в макете. Я хочу изменить значение EditText и TextView относительно другого значения EditText. И то же самое для другого editText. Я реализовал код, как показано ниже.

 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

        }
    });

Но пока я запускаю приложение, я получаю сообщение об ошибке типа stackoverflow.

ошибка Logcat:

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: 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: 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

Я не знаю, где я не прав. .
Пожалуйста, помогите мне реализовать такой метод. Благодаря.

1 Ответ

0 голосов
/ 08 октября 2011

попытайтесь уменьшить выполнение кода несколькими типами, скажем такя имею в виду попытаться удалить такой код, который является общим, чтобы процесс выполнения был компактным и мог привести к решению вашей проблемы

...