简体   繁体   English

检查editText中的文本是否已更改

[英]Check if text in editText has changed

I have two editTexts. 我有两个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. 当我尝试对第二个editText做同样的事情时,应用程序不移动,此后崩溃。 Here's the errorlog: 这是错误日志:

E/JavaBinder: !!! E / JavaBinder:!!! FAILED BINDER TRANSACTION !!! 绑定交易失败!!! (parcel size = 2057252) E/MQSEventManagerDelegate: reportJEEvent error happened:android.os.TransactionTooLargeException: data parcel size 2057252 bytes (包裹大小= 2057252)E / MQSEventManagerDelegate:发生reportJEEvent错误:android.os.TransactionTooLargeException:数据包裹大小2057252字节

you can add an edit text listener. 您可以添加编辑文本侦听器。 Put you logic on afterTextChanged 将逻辑放在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM