How to update the same EditText using TextWatcher?

前端 未结 4 915
故里飘歌
故里飘歌 2020-12-11 19:30

In my Android application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With

4条回答
  •  再見小時候
    2020-12-11 19:43

    The content of the TextView is uneditable on the onTextChanged event.

    Instead, you need to handle the afterTextChanged event to be able to make changes to the text.

    For more thorough explanation see: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged


    Note: Error onTextChanged

    Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.

    From the ref:

    public abstract void afterTextChanged (Editable s)
    This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. ...

    • Suggestion 1: if you can, check if the s is already what you want when the event is triggered.

      @Override
      public void afterTextChanged(Editable s)
      {    
          if( !s.equalsIngoreCase("smth defined previously"))
               s = "smth defined previously";              
      }
      
    • Suggestion 2: if you need to do more complex stuff (formatting, validation) you can maybe use a synchronized method like in this post.

    Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( ****four)

    You can use something like this in suggestion 1:

        @Override
        public void afterTextChanged(Editable s)
        {    
           String sText = ET.getText().toString()
    
            if( !isFormatted(sText))
                 s = format(sText);              
        }
        bool isFormatted(String s)
        {
         //check if s is already formatted
        }
    
        string format(String s)
        {
          //format s & return
        }
    

提交回复
热议问题