Edittext退格长时间保持不起作用

时间:2018-09-04 05:47:31

标签: android android-edittext

我有Edittext,其中已经添加了TextWatcher。当我按Backspace键时,它的工作原理是,但如果我按住Backspace键,则并不会删除所有文本。

public sTextWatcher implements TextWatcher{

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

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


    }

}

@Override
public void afterTextChanged(Editable s) {
        edittext.setText(s.toString());

}

1 个答案:

答案 0 :(得分:0)

尝试此代码

editText.addTextChangedListener( new TextWatcher() {
        boolean isEdiging;
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override public void afterTextChanged(Editable s) {
            if(isEdiging) return;
            isEdiging = true;

            String str = s.toString().replaceAll("[^\\d]", "");
            double s1 = 0;
            try {
                s1 = Double.parseDouble(str);
            }catch (NumberFormatException e){
                e.printStackTrace();
            }


            NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
            ((DecimalFormat)nf2).applyPattern("###,###.###");
            s.replace(0, s.length(), nf2.format(s1));

            isEdiging = false;
        }    editText.addTextChangedListener( new TextWatcher() {
        boolean isEdiging;
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override public void afterTextChanged(Editable s) {
            if(isEdiging) return;
            isEdiging = true;

            String str = s.toString().replaceAll("[^\\d]", "");
            double s1 = 0;
            try {
                s1 = Double.parseDouble(str);
            }catch (NumberFormatException e){
                e.printStackTrace();
            }


            NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
            ((DecimalFormat)nf2).applyPattern("###,###.###");
            s.replace(0, s.length(), nf2.format(s1));

            isEdiging = false;
        }
    });
    });