2017-01-19 7 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

はい。それはドキュメントのようだ。 –

関連する問題