fesave
fesave

Reputation: 191

How to clear EditText when it has focus and any keyboard key is pressed

I am trying to clean EditText when it has focus and the soft keyboard appears and any key is pressed. I am using OnFocusChangeListener method to detect when EditText has focus, but I do not know how to implement the keyboard event in the same method.

Can anyone help me? Thank you. Kind Regards

Upvotes: 1

Views: 156

Answers (1)

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

You can use also use the setOnFocusChangeListener of the Editext

 private boolean isFirstTime = true;

    FIRST_EDITTEXT.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
              if(s.toString().length()>0 && isFirstTime ){
                FIRST_EDITTEXT.setText("");
                isFirstTime = false;
               }
        }

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

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

Now if we are one the SECOND EditText we will true the isFirstTime boolean , for the First EditText like below.

 SECOND_EDITTEXT.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                isFirstTime = true;

            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

Upvotes: 1

Related Questions