Android: why i am not able to change the value of EditText with respect to another EditText?

ぐ巨炮叔叔 提交于 2019-12-03 21:29:26

问题


I am going to change the text of the edittext based on the value enter on the another edittext. and also like same thig with visa-versa.

For that i have use the TextChanged Listener and implemented as like below:

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

But after doing this i am not able to get the text change. for Secod EditText its worked nice but while i use the First EditText then it gives me error at the commented line.

Error Log:

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

So whats the wrong with this ? Why i am not able to get effect for both the EditText ? Thanks.


回答1:


this is because you are doing setText() inside onTextChanged . So when you change the text onTextChanged will be called and again inside onTextChanged you are calling setText(). So thr text changed again and onTextChanged() will be called again and this process will continue till stack overflow and result to stackOverFlow error. Hope you can understand...

user removeTextChangedListener() before setText().

Edit:

Define
TextWatcher excludeTW,includeTW; //globaly.

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



回答2:


I think this is causing you problem because you are putting the whole code in recursion. i.e 1st editext changes the text of 2nd edittext which again changes the text of 1st edittext...this goes on forever..

Solution: apply some logic that adds the text change listener when the 1st edit text has lost its focus n vice versa.

Comment if u need help.

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

This will definately work.




回答3:


You seem to have hooked up an infinite recursion with both the edit text.

onTextChanged() of includedText calls excludedText.setText() which triggers the onTextChanged on excludedText which again calls includedText.setText(). And this repeats on forever until it crashes.

Hence the Stackoverflow. See here for solution

Edit: I haven't compiled it, just edited it as is. See here




回答4:


below code will work 100 percent try it declare 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) {

                    }
                }); 


            }
        }); 


来源:https://stackoverflow.com/questions/8600587/android-why-i-am-not-able-to-change-the-value-of-edittext-with-respect-to-anoth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!