How to use the TextWatcher class in Android?

前端 未结 9 1029
独厮守ぢ
独厮守ぢ 2020-11-22 02:30

Can anyone tell me how to mask the substring in EditText or how to change EditText substring input to password type

9条回答
  •  攒了一身酷
    2020-11-22 02:55

    Using TextWatcher in Android

    Here is a sample code. Try using addTextChangedListener method of TextView

    addTextChangedListener(new TextWatcher() {
    
            BigDecimal previousValue;
            BigDecimal currentValue;
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int
                    count) {
                if (isFirstTimeChange) {
                    return;
                }
                if (s.toString().length() > 0) {
                    try {
                        currentValue = new BigDecimal(s.toString().replace(".", "").replace(',', '.'));
                    } catch (Exception e) {
                        currentValue = new BigDecimal(0);
                    }
                }
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                if (isFirstTimeChange) {
                    return;
                }
                if (s.toString().length() > 0) {
                    try {
                        previousValue = new BigDecimal(s.toString().replace(".", "").replace(',', '.'));
                    } catch (Exception e) {
                        previousValue = new BigDecimal(0);
                    }
                }
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
                if (isFirstTimeChange) {
                    isFirstTimeChange = false;
                    return;
                }
                if (currentValue != null && previousValue != null) {
                    if ((currentValue.compareTo(previousValue) > 0)) {
                        //setBackgroundResource(R.color.devises_overview_color_green);
                        setBackgroundColor(flashOnColor);
                    } else if ((currentValue.compareTo(previousValue) < 0)) {
                        //setBackgroundResource(R.color.devises_overview_color_red);
    
                        setBackgroundColor(flashOffColor);
                    } else {
                        //setBackgroundColor(textColor);
                    }
                    handler.removeCallbacks(runnable);
                    handler.postDelayed(runnable, 1000);
                }
            }
        });
    

提交回复
热议问题