-2

I code like this in below. But it doesn't work. I need to perform simple addition and simple subtraction based on which two of those field have input.... Question ends...

I needed to make this text because I can't ask the question if details isn't enough and code is more than details. so I'm just typing this to ask my question. Just ignore it and see the code below. Thank you

Java Code :

package com.test.easycount;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements TextWatcher {

    EditText num1, num2, sum;
    int fnumber, snumber, total;

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

        num1 = (EditText) findViewById(R.id.num1);
        num2 = (EditText) findViewById(R.id.num2);
        sum = (EditText) findViewById(R.id.sum);

        num1.addTextChangedListener(this);
        num2.addTextChangedListener(this);
        sum.addTextChangedListener(this);

        fnumber = Integer.parseInt(num1.getText().toString());
        snumber = Integer.parseInt(num2.getText().toString());
        total = Integer.parseInt(sum.getText().toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {

        if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {

            total = fnumber + snumber;
            sum.setText(Integer.toString(total));

        } else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {

            snumber = total - fnumber;
            num2.setText(Integer.toString(snumber));
        } else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {

            fnumber = total - snumber;
            num1.setText(Integer.toString(fnumber));
        }
    }
}

Please help me to do this....

Shohan Ahmed Sijan
  • 4,391
  • 1
  • 33
  • 39
jde
  • 9

2 Answers2

0

remove initializing variables and paste them in afterTextChanged

  @Override
            public void afterTextChanged(Editable s) {

        try {
             fnumber = Integer.parseInt(num1.getText().toString());
        } catch (NumberFormatException e) {
            fnumber = 0;
            Log.e("numberFormat", e.toString());
        }

        try {
            snumber = Integer.parseInt(num2.getText().toString());
        } catch (NumberFormatException e) {
            snumber = 0;
            Log.e("numberFormat", e.toString());
        }

        try {
            total = Integer.parseInt(sum.getText().toString());
        } catch (NumberFormatException e) {
            total = 0;
            Log.e("numberFormat", e.toString());
        }

      if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {

                    total = fnumber + snumber;
                    sum.setText(Integer.toString(total));

                } else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {

                    snumber = total - fnumber;
                    num2.setText(Integer.toString(snumber));
                } else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {

                    fnumber = total - snumber;
                    num1.setText(Integer.toString(fnumber));
                }
            }
Hamidreza Samadi
  • 637
  • 1
  • 7
  • 24
  • No. It doesn't work... Application crashed while enter values in EditText. What should I do now??? Assign values for other EditText? because that's null while I enter first value. How can I give validation?? Can you please help? – jde Aug 20 '17 at 05:12
0

You have to calculate fnumber, snumber, total in textWatcher :

Suppose:

    @Override
    public void afterTextChanged(Editable s) {

        if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {
            fnumber = Integer.parseInt(num1.getText().toString());
            snumber = Integer.parseInt(num2.getText().toString());
            total = fnumber + snumber;
            sum.setText(Integer.toString(total));

        } else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
            fnumber = Integer.parseInt(num1.getText().toString());
            total = Integer.parseInt(sum.getText().toString());
            snumber = total - fnumber;
            num2.setText(Integer.toString(snumber));

        } else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
            snumber = Integer.parseInt(num2.getText().toString());
            total = Integer.parseInt(sum.getText().toString());
            fnumber = total - snumber;
            num1.setText(Integer.toString(fnumber));
        }
}

And remove fnumber, snumber, total initialization from onCreate.

For easy calculation it's better to use separate textWatcher for each edittext:

Ex:

num1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

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

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
Shohan Ahmed Sijan
  • 4,391
  • 1
  • 33
  • 39