安卓:为什么我不能够改变的EditText的价值相对于另一些的EditText? [英] Android: why i am not able to change the value of EditText with respect to another EditText?

查看:166
本文介绍了安卓:为什么我不能够改变的EditText的价值相对于另一些的EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变的基础上的价值的EditText文本上的另一个的EditText进入。并且也喜欢同THIG用,反之亦然。

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.

有关,我有使用框TextChanged监听器,并实现为象下面这样:

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

不过,这样做后,我不能够获取文本的变化。对于Secod的EditText它的工作不错,但同时我用第EditText上,然后它给了我错误的注释行。

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.

错误日志:

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

因此​​,whats的问题呢?为什么我不能够得到效果都的EditText? 谢谢你。

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

推荐答案

这是因为你正在做的的setText ()内的 onTextChanged 。因此,当您更改文本onTextChanged将被调用,并再次内部onTextChanged您所呼叫的的setText ()。所以THR文本再次改变和 onTextChanged ()将被再次调用,这个过程将持续到堆栈溢出,从而导致以StackOverflow的错误。希望你能理解...

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

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

修改

定义
TextWatcher excludeTW,includeTW; // globaly

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

这篇关于安卓:为什么我不能够改变的EditText的价值相对于另一些的EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆