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