2017-01-19 6 views
2

Я читал, что OnKeyListener предназначен для обнаружения жестких клавиш и OnEditorActionListener для программных клавиш. Однако в Интернете есть много примеров, поэтому кажется, что они оба работают нормально в любой ситуации.Какая разница между OnKeyListener и OnEditorActionListener?

Я пытался как и OnKeyListener работает только на реальном устройстве в то время как OnEditorActionListener работ на реальном устройстве и эмулятор тоже.

Из-за этого я хочу знать разницу между ними и выяснить, когда использовать любой из них. Буду признателен, если кто-нибудь сможет мне объяснить.

ответ

2

Просто маленькая копия паста из документации:

/** 
    * 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); 
    } 

Не думайте, что метод ввода программного обеспечения должна быть ключом на основе; даже если он , он может использовать нажатия клавиш по-другому, чем вы ожидаете, поэтому нет способа надежно улавливать мягкие нажатия клавиш ввода.

Аналогично,

/** 
    * 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; 
    } 
+1

Таким образом, это означает, что 'OnEditorActionListener' предпочтительнее' 'OnKeyListener? Потому что в некоторых случаях последнее может не срабатывать? – Marat

+0

Да. Это похоже на документы. –