user10524436
user10524436

Reputation:

Check if text in editText has changed

I have two editTexts. I want to test every time if the text has been changed and if it's the case I want to change the text of the second one too.

Here is what I've done :

TextWatcher fieldValidatorTextWatcherElec = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        if (filterLongEnough1()) {
            et_electricite_€.setText(String.valueOf(new BigDecimal(Double.parseDouble(et_electricite.getText().toString())*tarif).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        }
    }

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

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

    }

    private boolean filterLongEnough1() {
        return et_electricite.getText().toString().trim().length() > 0;
    }
};
et_electricite.addTextChangedListener(fieldValidatorTextWatcherElec);

TextWatcher fieldValidatorTextWatcherElecTarif = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        if (filterLongEnough()) {
            et_electricite.setText(String.valueOf(new BigDecimal(Double.parseDouble(et_electricite_€.getText().toString())/tarif).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        }
    }

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

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

    }

    private boolean filterLongEnough() {
        return et_electricite_€.getText().toString().trim().length() > 0;
    }
};
et_electricite_€.addTextChangedListener(fieldValidatorTextWatcherElecTarif);

The problem is: When I click for the first time it changes; when I try to do the same thing with the second editText the application doesn't move and after that, it crashes. Here's the errorlog:

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 2057252) E/MQSEventManagerDelegate: reportJEEvent error happened:android.os.TransactionTooLargeException: data parcel size 2057252 bytes

Upvotes: 2

Views: 1486

Answers (1)

Rubin bhandari
Rubin bhandari

Reputation: 1951

you can add an edit text listener. Put you logic on afterTextChanged

EditText answer = new EditText(this);

//second, we create the TextWatcher
TextWatcher textWatcher = 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) {
       //here, after we introduced something in the EditText we get the string from it
       String answerString = answer.getText().toString();

       //and now we make a Toast
       //modify "yourActivity.this" with your activity name .this
       Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();

    }
};

//third, we must add the textWatcher to our EditText
answer.addTextChangedListener(textWatcher);

Upvotes: 2

Related Questions