Domanda

Ok, I was able do auto populate 3 fields with one field with TextWatcher, but I would get an error if I tried to backspace the number and replace it with another, I cannot seem to get the TextWatcher to watch the other fields

@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));
                        }
                    });
                }
            });
        }
    });
}}        
È stato utile?

Soluzione

As you are using this

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

But when there is No text in TextField it will try to do the above stuff which is don't allowed as it'll generate exception.

  1. Surround your Code with try catch block to handle exception.
  2. You can put condition like this

    if(lengthofText!=0) { //Then do your stuff } else{ //Nothing }

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top