Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact difference between OnKeyListener and OnEditorActionListener?

I have read that OnKeyListener is for detecting hard keys and OnEditorActionListener for soft keys. However, there are many examples in the internet, therefore it seems that they both work OK in any situation.

I have tried both and OnKeyListener works only on real device while OnEditorActionListener works on real device and emulator too.

Because of this, I want to know the difference between them and to figure out when to use either of them. I would appreciate if anyone could explain me.

like image 439
Marat Avatar asked Jan 19 '17 07:01

Marat


People also ask

How do I use OnKeyListener?

Stay organized with collections Save and categorize content based on your preferences. Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view.

What is setOnKeyListener?

setOnKeyListener - Callback for pressing a hardware key when view has focus. setOnLongClickListener - Callback for pressing and holding a view. setOnTouchListener - Callback for touching down or up on a view.

Which class contains the Onkey method in Android?

MainActivity class implements the interface OnKeyListener .


1 Answers

Just a small copy paste from the docs:

 /**
     * Interface definition for a callback to be invoked when a hardware key event is
     * dispatched to this view. The callback will be invoked before the key event is
     * given to the view. This is only useful for hardware keyboards; a software input
     * method has no obligation to trigger this listener.
     */
    public interface OnKeyListener {
        /**
         * Called when a hardware key is dispatched to a view. This allows listeners to
         * get a chance to respond before the target view.
         * <p>Key presses in software keyboards will generally NOT trigger this method,
         * although some may elect to do so in some situations. Do not assume a
         * software input method has to be key-based; even if it is, it may use key presses
         * in a different way than you expect, so there is no way to reliably catch soft
         * input key presses.
         *
         * @param v The view the key has been dispatched to.
         * @param keyCode The code for the physical key that was pressed
         * @param event The KeyEvent object containing full information about
         *        the event.
         * @return True if the listener has consumed the event, false otherwise.
         */
        boolean onKey(View v, int keyCode, KeyEvent event);
    }

Do not assume a software input method has to be key-based; even if it is, it may use key presses in a different way than you expect, so there is no way to reliably catch soft input key presses.

Similarly,

   /**
     * Set a special listener to be called when an action is performed
     * on the text view.  This will be called when the enter key is pressed,
     * or when an action supplied to the IME is selected by the user.  Setting
     * this means that the normal hard key event will not insert a newline
     * into the text view, even if it is multi-line; holding down the ALT
     * modifier will, however, allow the user to insert a newline character.
     */
    public void setOnEditorActionListener(OnEditorActionListener l) {
        createEditorIfNeeded();
        mEditor.createInputContentTypeIfNeeded();
        mEditor.mInputContentType.onEditorActionListener = l;
    }
like image 189
Eric B. Avatar answered Oct 17 '22 06:10

Eric B.