How to use the TextWatcher class in Android?

前端 未结 9 1125
独厮守ぢ
独厮守ぢ 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

    Create custom TextWatcher subclass:

    public class CustomWatcher implements TextWatcher {
    
        private boolean mWasEdited = false;
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
            if (mWasEdited){
    
                mWasEdited = false;
                return;
            }
    
            // get entered value (if required)
            String enteredValue  = s.toString();
    
            String newValue = "new value";
    
            // don't get trap into infinite loop
            mWasEdited = true;
            // just replace entered value with whatever you want
            s.replace(0, s.length(), newValue);
    
        }
    }
    

    Set listener for your EditText:

    mTargetEditText.addTextChangedListener(new CustomWatcher());
    

提交回复
热议问题