Reputation: 1853
I have multiple edittext
and a textview
in the bottom in order to get the sum of what is written in the edittexts
but I didn't succeded. I tried the TextWatcher
but it doesn't do the work, for exemple i write in the first one 5 and in the second 10 I got 16 as a result as it counts 5+1+10. I also tried the onfocus
method but the problem still with the last edittext
it doesn't update the result. I really don't know what to do. Someone had this issue before?
TextWatcher implementation:
TextWatcher inputTextWatcher1 = new TextWatcher() {
public void afterTextChanged(Editable s) {
number = Integer.parseInt(s.toString());
sum+=number;
tv.setText("sum is "+sum);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(final CharSequence s, int start, int before,
int count) {
}
};
Upvotes: 4
Views: 9968
Reputation: 26034
Use following code. I have tested it and working like charm..
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (s.length() > 0) {
int number = Integer.parseInt(s.toString());
ans -= number;
}
}
@Override
public void afterTextChanged(Editable s) {
int number = Integer.parseInt(s.toString());
ans += number;
txtOutput.setText("sum is " + ans);
}
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (s.length() > 0) {
int number = Integer.parseInt(s.toString());
ans -= number;
}
}
@Override
public void afterTextChanged(Editable s) {
int number = Integer.parseInt(s.toString());
ans += number;
txtOutput.setText("sum is " + ans);
}
});
Here, ans
must be 0
before use. et1
and et2
are EditText
.
Logic
beforeTextChanged
will call before any text changes applied. Like you type 5, then it returns "" as there is no data, then if you type 2, it returns 5 as it was entered earlier. So this is the trick.
Before adding any value, just subtract it from previous one. Otherwise your logic is true. but every time you are adding number in answer. Which make your answer false.
Benefit
Now, if you use this logic, there doesn't matter how many EditText
there. This will provide true answer. You just need to add TextWatcher
to every EditText.
Upvotes: 6
Reputation: 3476
Lets say - editText1 - firstNumber & editText2- secondNumber
TextWatcher inputTextWatcher1 = new TextWatcher() {
public void afterTextChanged(Editable s) {
number1 = Integer.parseInt(editText1.getText().toString());
number2 = Integer.parseInt(editText2.getText().toString());
sum = number1 + number2;
tv.setText("sum is "+sum);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(final CharSequence s, int start, int before,
int count) {
}
};
Upvotes: 0