TextWatcher不处理多个TextEdit字段

时间:2014-03-30 01:58:00

标签: java android textwatcher

好吧,我可以使用TextWatcher自动填充3个字段和一个字段,但如果我尝试对数字进行退格并将其替换为另一个字段,我会收到错误,我似乎无法让TextWatcher查看其他字段

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editIn = (EditText) findViewById(R.id.editIn);
    final EditText editFt = (EditText) findViewById(R.id.editFt);
    final EditText editYd = (EditText) findViewById(R.id.editYd);

    editIn.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)                                                      
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            double in = Double.valueOf(editIn.getText().toString());
            double ft = in / 12;
            double yd = in / 36;
            editFt.setText(String.valueOf(ft));
            editYd.setText(String.valueOf(yd));

            editFt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int  i3) {      
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    double ft = Double.valueOf(editFt.getText().toString());
                    double in = ft * 12;
                    double yd = ft / 3;
                    editIn.setText(String.valueOf(in));
                    editYd.setText(String.valueOf(yd));

                    editYd.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int                                        i2, int i3) {
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            double yd = Double.valueOf(editYd.getText().toString());
                            double in = yd * 36;
                            double ft = yd * 3;
                            editIn.setText(S`enter code here`tring.valueOf(in));
                            editFt.setText(String.valueOf(ft));
                        }
                    });
                }
            });
        }
    });
}}        

1 个答案:

答案 0 :(得分:0)

正如您使用此

double ft = Double.valueOf(editFt.getText().toString());

但是当TextField中没有文字时,它会尝试执行上述不允许的内容,因为它会产生异常。

  1. 使用try catch块覆盖您的代码以处理异常。
  2. 你可以放这样的条件

    如果(lengthofText!= 0) { //然后做你的东西 } 其他{ //没有 }