Android: Using DecimalFormat in TextWatcher with Edittext

489 Views Asked by At

I have a problem with using decimal format in textwatcher. If I don't use DecimalFormat in TextWatcher, there is no problem in the program. I can delete and set all edittext (above codes/photo left) But if I use, the program stops.(below codes/photo right) For example, when I enter the value in the first edittext and go to the second edittext, the program stops. I also tried the code in afterTextChanged and try-catch but there was no change. How can i solve this problem?

the_photo

etType5.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (getCurrentFocus() == etType5) {
                if (etType5.length() != 0) {
                    double minute = Double.parseDouble(etType5.getText().toString());                      
                    double second = minute * 60.0;                   
                    etType6.setText(String.valueOf(second));
                } else {
                   etType6.setText("");
                }
            }
        }

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

    etType6.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (getCurrentFocus() == etType6) {
                if (etType6.length() != 0) {
                    double second = Double.parseDouble(etType6.getText().toString());
                    double minute = second * 0.016666667;
                    etType5.setText(String.valueOf(minute));
                } else {
                    etType5.setText("");
                }
            }
        }

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

etType5.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
    DecimalFormat df = new DecimalFormat("#.###");

            if (getCurrentFocus() == etType5) {
                if (etType5.length() != 0) {
                    double minute = Double.parseDouble(etType5.getText().toString());                      
                    double second = minute * 60.0;                   
                    etType6.setText(df.format(second));
                } else {
                   etType6.setText("");
                }
            }
        }

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

    etType6.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
    DecimalFormat df = new DecimalFormat("#.###");

            if (getCurrentFocus() == etType6) {
                if (etType6.length() != 0) {
                    double second = Double.parseDouble(etType6.getText().toString());
                    double minute = second * 0.016666667;
                    etType5.setText(df.format(minute));
                } else {
                    etType5.setText("");
                }
            }
        }

        public void afterTextChanged(Editable s) {
        }
    });
0

There are 0 best solutions below