B
B
brucebanner2016-04-23 05:51:27
Java
brucebanner, 2016-04-23 05:51:27

The processing of pressing Enter from the keyboard does not work. Only numbers are processed!?

I found several examples about this, somewhere with switch somewhere with if, but that's not the point, it doesn't work anyway. The handler reacts only to pressing numbers. Even default in switch`e does not react. What am I doing wrong? Here is the code

edit.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (KeyEvent.ACTION_DOWN == event.getAction()){
                switch (keyCode){
                    case KeyEvent.KEYCODE_ENTER:
                        Log.e("TAG", "Нажат интер");
                        break;
                    case KeyEvent.KEYCODE_2:
                        Log.e("TAG", "Нажато 2");
                        break;
                    case KeyEvent.KEYCODE_E:
                        Log.e("TAG", "Нажато e");
                        break;
                    default: Log.e("TAG", "Нажато что то");
                }
            }
            return false;
        }
    });

The whole handler reacts only to numbers. I press letters or some other symbols default does not work, I press numbers except 2 - it works. I press 2 - it works. All keys poked "Pressed e" does not come out. And inter too. Maybe it's the phone model I don't know, I have a Lenovo S650 Android 4.4.2 Api 19. There is no other phone to check.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
brucebanner, 2016-04-25
@brucebanner

I did that too, it doesn't help. As a result, I did it with the help of TextWatcher. They just said he does not process ENTER, he searched for "\n". Here is the code:

edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!s.toString().contains("\n")) {
                    Log.e("TAG", "Первая строка: Печатается");
                } else {
                    str = s.toString().split("\n");
                    Log.e("TAG", "Первая строка: " + str[0]);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

I needed to remove the first line. Voot did and without processing ENTER

A
Andrey Myvrenik, 2016-04-24
@gim0

You're handling clicks incorrectly. View.OnKeyListener#onKey is called when a button on the physical keyboard is pressed. Reacting to pressing numbers on the software keyboard is probably one of the exceptions mentioned in the documentation:
Handle clicks in EditText like this:

// EditText edit = ..;
edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN) {
                switch(event.getKeyCode()) {
                    case KeyEvent.KEYCODE_ENTER:
                        // Нажат Enter
                        break;
                }
            }
            return false;
        }
    });

See TextView.OnEditorActionListener#onEditorAction and also keep in mind that the return value (true/false) matters too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question