1

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

fesave
  • 181
  • 13

1 Answers1

1

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) {
            }
        });
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • Hi Ravindra, your solution is not valid for me, I am going to repdroduce my case: 1-Write on the EditText 2-Change focus to other EditText 3-Focus in the EditText of step 1, the keyboard appears, tap in any key and EditText cleans the previous text, and shows the new one. – fesave Sep 12 '18 at 11:58
  • @MachuSnz have a look on the updated solution and let me know in case of concern – Ravindra Kushwaha Sep 12 '18 at 12:16
  • I'm sorry for the wait, I've reviewed your solution, it's still not working, thanks anyway. – fesave Sep 12 '18 at 14:37
  • Sorry yet now I am out of office , tomorrow will definitely help..what problem yet now u facing .can u guide me...I will try it tomorrow – Ravindra Kushwaha Sep 12 '18 at 15:13
  • @MachuSnz have u resolved the problem...let me know in case of concern – Ravindra Kushwaha Sep 13 '18 at 09:12
  • Hi Ravindra, I think that I have not explained it correctly. Imagine that you have a screen with two EditText, the first one, is for your name, and the second one is for your last name, ok?. The first step is write your name, you have wrote it and you go to the second step: write your last name. But you have an error in your name and you want to solve it. You click in the first EditText (for your name) who has the focus automatically and the keyboard appears, you click in any key for fix your name and automatically the EditText for your name cleans the previous text and show the new the one. – fesave Sep 13 '18 at 09:12
  • @MachuSnz In above example , i have did same as you want..I am using there ** FIRST_EDITTEXT** is your **name edittext** and **SECOND_EDITTEXT** is your surname editext.... What i have done applied `setOnFocusChangeListener` on your **first editext(name)** and applied the coditon like `if (hasFocus && isFirstTime) ` for clearing the Editext ok....Now i am using the `addTextChangedListener ` for the **second editext(surname)** , inside it i am using the `isFirstTime = true;` for the **first editext**......From this logic what happen..when user write down in second editext and goes to first – Ravindra Kushwaha Sep 13 '18 at 09:20
  • **continue....** editext than first editext become empty...Did u get it or not...have u tried above code?? – Ravindra Kushwaha Sep 13 '18 at 09:21
  • I have updated the name of the Editext for your better understanding – Ravindra Kushwaha Sep 13 '18 at 09:24
  • yeah, but you clean the first EditText when it gains the focus from the second EditText, not when user start to write on it. – fesave Sep 13 '18 at 09:25
  • You can also apply `addTextChangedListener ` on the FIRST_EDITEXT and check like inside the `onTextChanged ` method like **if(s.toString().length()>0){ FIRST_EDITEXT.setText("")**} here `s` is the ** CharSequence** of the method `onTextChanged`.... Is it make some sense..Try it and let me know in case of concern – Ravindra Kushwaha Sep 13 '18 at 09:28
  • Welcome friend...Sorry it takes time to give u a perfect solution..Keep happy coding dost – Ravindra Kushwaha Sep 13 '18 at 09:30
  • No problem, you are a great dev, have a nice day! see you soon! – fesave Sep 13 '18 at 09:31
  • @MachuSnz ..One more thing which i forget that inside your first editext, u need to apply condition with boolean like **if(s.toString().length()>0 && isFirstTime ){ FIRST_EDITEXT.setText(""); isFirstTime = false; }** . ....Got it friend..Or get some problem – Ravindra Kushwaha Sep 13 '18 at 09:36
  • Got it! Thanks again. – fesave Sep 13 '18 at 09:38
  • ...Edited the solution for it for the better understand – Ravindra Kushwaha Sep 13 '18 at 09:39