user3241467
user3241467

Reputation: 1

How do I get the result of the sum of several EditText with TextWacher?

How I can get the result of the sum of several EditText in a TextView at the moment with so TextWatcher adding them to leave as they placed the numbers in EditText

    edit1.addTextChangedListener(mTextEditorWatcher);
    edit2.addTextChangedListener(mTextEditorWatcher);
    edit3.addTextChangedListener(mTextEditorWatcher);
}

private final TextWatcher  mTextEditorWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {

    }

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

    }

    public void afterTextChanged(Editable s)
    {
        String change;
        int num= 0;
        int n = 0;
        change = s.toString();
        num =  Integer.parseInt(change);
        n = n + num;
        Posi.setText(n); 
    }
};

Upvotes: 0

Views: 174

Answers (1)

SimonSays
SimonSays

Reputation: 10977

Your TextWatcher needs to know what EditTexts it needs to sum. You could, for example, pass them in the constructor.

private class MyTextWatcher implements TextWatcher {
    private List<EditText> editTexts;

    public MyTextWatcher(EditText et1, EditText et2, EditText et3){
        ....
    }

    // or
    public MyTextWatcher(List<EditText> editTexts){
        this.editTexts = editTexts;
    }

    ....
}

Upvotes: 1

Related Questions