V
V
Vanes Ri_Lax2015-08-18 09:32:40
Java
Vanes Ri_Lax, 2015-08-18 09:32:40

How to trace backspace key?

Hello, I have an EditText field. You can only enter whole numbers in it. I need to activate the button if the length of the entered data is greater than 0 and accordingly block if it is equal to zero.
I am trying to do like this:

editNumberRace = (EditText)findViewById(R.id.editText2);

        View.OnKeyListener onKeyListener = new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if(editNumberRace.toString().length()>0){
                    //делаем что то
                }
                else {
                    //делаем что то
                }
                return false;
            }
        };

        editNumberRace.setOnKeyListener(onKeyListener);

Everything works if you enter data, but if you delete characters by pressing the Backspace key, then for some reason no event occurs in principle. From this it turns out that if this key is used to delete the entire line, then my button is not blocked.
How can I implement this?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-08-18
@vanesxl

You don't need to track. You just need to use the right tools. To work with EditText there is a special TextWatcher .

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question