SoFunction
Updated on 2025-03-01

Implementation example code of Android clear edit box content function

introduction

In project development, you will definitely encounter such needs. It is too troublesome to clean up the data entered into EditText one by one. You need a button to directly delete the data in the entire EditText control at one time. Then I will encapsulate a method for you and call it directly if you have such needs. Without further ado, just upload the code:

/**
 *Clear the edit box button
 * @param editText EditText control that needs to be cleared
 * @param delImageView Clear data picture
 */ 
protected void clearEditText(final EditText editText, final ImageView delImageView) { 
/Listening for text box editing/ 
(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
      // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                   int arg3) {
      // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
      // TODO Auto-generated method stub
      if(().length()!=0){
        ();
      } else {
        ();
      }
    }
  });
  /**Focus change monitoring**/
  (new () {
    @Override
    public void onFocusChange(View arg0, boolean arg1) {
      // TODO Auto-generated method stub
      if(().length()!=0){
      //Delete icon display        ();
      } else {
      //Delete icon to hide        ();
      }
      if(arg1){
        //Get focus      } else {
        //Lost focus, delete icon hide        ();
      }
    }
  });
  //Delete the icon click and listen event  (new () {

    @Override
    public void onClick(View arg0) {
      // Execute clear EditText data      ("");
    }
  });
}

End, just call it where needed according to the needs.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.