4

我将根据在另一个编辑文本上输入的值更改编辑文本的文本。和反之亦然。

为此,我使用了 TextChanged 侦听器并按如下方式实现:

 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);

                    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);
                    excludedText.setText(excluded); //////// Error Line
                }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });


    // worked
    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);
            }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

但是在这样做之后,我无法更改文本。对于 Secod EditText,它工作得很好,但是当我使用 First EditText 时,它在注释行处给了我错误。

错误日志:

12-22 13:08:17.640: ERROR/AndroidRuntime(1077): FATAL EXCEPTION: main
12-22 13:08:17.640: ERROR/AndroidRuntime(1077): java.lang.StackOverflowError
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.text.TextUtils.getChars(TextUtils.java:69)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.text.TextUtils.indexOf(TextUtils.java:102)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.text.StaticLayout.generate(StaticLayout.java:131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.makeNewLayout(TextView.java:4851)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.checkForRelayout(TextView.java:5348)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2688)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$1.afterTextChanged(GSTActivity.java:74)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2695)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:93)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2691)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2556)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.EditText.setText(EditText.java:75)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at android.widget.TextView.setText(TextView.java:2531)
12-22 13:08:17.640: ERROR/AndroidRuntime(1077):     at com.project.TaxCalculator.GSTAct

那么这有什么问题呢?为什么我无法同时对 EditText 生效?谢谢。

4

4 回答 4

14

这是因为您在onTextChanged中执行setText () 。因此,当您更改文本时,将调用 onTextChanged 并再次在 onTextChanged 内调用setText ()。所以 thr 文本再次改变了 onTextChanged () 将再次被调用,此过程将继续直到堆栈溢出并导致 stackOverFlow 错误。希望你能明白...

setText ()之前的用户 removeTextChangedListener() )。

编辑:

定义
TextWatcher excludeTW,includeTW;//全局。

    includedText = (EditText)findViewById(R.id.include);
    excludedText = (EditText)findViewById(R.id.exclude);

    includeTW = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
             if(!(includedText.getText().toString().equals("")))
                {
                    excludedText.removeTextChangedListener(excludeTW);
                    String included = includedText.getText().toString();
                    excludedText.setText(included); //////// Error Line
                }

        }



    };


    excludeTW = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        includedText.removeTextChangedListener(includeTW);
        String excluded = excludedText.getText().toString();
        includedText.setText(excluded);

        }



    };
    includedText.addTextChangedListener(includeTW) ;
    excludedText.addTextChangedListener(excludeTW);
于 2011-12-22T08:00:32.807 回答
1

我认为这会导致您出现问题,因为您将整个代码置于递归中。即第一个edittext 更改了第二个edittext 的文本,这再次更改了第一个edittext 的文本......这将永远持续下去......

解决方案:应用一些逻辑,在第一次编辑文本失去焦点时添加文本更改侦听器,反之亦然。

如果您需要帮助,请发表评论。

setOnTextChangeListener(true);

    excludedTextWatch = 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);
                includedText.setText("" + included);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    includedTextWatch = 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);

                String amt = String.valueOf(roundTwoDecimals(included
                        - (included / 1.15)));
                String excluded = String.valueOf(included / 1.15);
                System.out.println("The Amount is: " + amt);
                excludedText.setText("" + excluded); // ////// Error
                                                        // Line
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    includedText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            excludedText.removeTextChangedListener(excludedTextWatch);
            includedText.removeTextChangedListener(includedTextWatch);
            setOnTextChangeListener(hasFocus);
        }
    });
}

private void setOnTextChangeListener(boolean shouldSetTheListener) {
    if (shouldSetTheListener) {
        includedText.addTextChangedListener(includedTextWatch);
    } else {
        excludedText.addTextChangedListener(excludedTextWatch);
    }
}

这肯定会奏效。

于 2011-12-22T07:59:16.737 回答
1

您似乎已经将两个编辑文本都连接到了无限递归。

onTextChanged() 的includedText调用excludedText.setText()触发 onTextChangedexcludedText再次调用includedText.setText()。这会永远重复,直到它崩溃。

因此,Stackoverflow。解决方法见这里

编辑:我没有编译它,只是按原样编辑它。看这里

于 2011-12-22T08:00:01.287 回答
0

下面的代码将工作 100% 尝试它声明 globaly TextWatcher t1,t2;

         final EditText myOutputBox = (EditText) ConvertView1.findViewById(R.id.editTextbackrate);

           myOutputBox.setRawInputType(Configuration.KEYBOARD_12KEY);

           final EditText myOutputBox1 = (EditText) ConvertView1.findViewById(R.id.edittextlayrate);

           myOutputBox.setRawInputType(Configuration.KEYBOARD_12KEY);

           myOutputBox1.addTextChangedListener(t1 = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub


                   if(s.toString().equalsIgnoreCase(""))
                   {
                       myOutputBox.setText("");
                  }
                   else
                   {

                           myOutputBox.removeTextChangedListener(t2);

                           String x = myOutputBox1.getText().toString();
                           myOutputBox.setText(x);

                               }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub




                myOutputBox.addTextChangedListener(t2 = new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        // TODO Auto-generated method stub

                               if(s.toString().equalsIgnoreCase(""))
                               {
                                    Log.i("t2", "1");
                                   myOutputBox1.removeTextChangedListener(t1);
                                   myOutputBox1.setText("");

                               }
                               else
                               {




                                       myOutputBox1.removeTextChangedListener(t1);
                                       String x = myOutputBox.getText().toString();
                                       myOutputBox1.setText(x);


                                           }


                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                            int after) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                }); 


            }
        }); 
于 2015-03-18T06:43:52.910 回答