OnKeyListener和OnEditorActionListener之间的确切区别是什么?

时间:2017-01-19 07:42:05

标签: android android-edittext keyboard

我已经读过OnKeyListener用于检测硬键,OnEditorActionListener用于检测软键。但是,互联网上有很多例子,因此看起来它们在任何情况下都能正常工作。

我已经尝试了两种方式,而OnKeyListener仅在真实设备上工作,而OnEditorActionListener也适用于真实设备和模拟器

因此,我想知道它们之间的区别,并想知道何时使用其中任何一个。如果有人能解释我,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

只需从文档中复制一小段内容:

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

不要假设软件输入法必须是基于密钥的;即使它   是的,它可能以不同于您预期的方式使用按键,所以   没有办法可靠地捕捉软输入键按下。

类似地,

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="nt:unstructured"
    sling:resourceType="my/another/component">
    <label
        jcr:primaryType="nt:unstructured"
        sling:resourceType="my/label/component"
        someProperty="myPropertyValue" />
</jcr:root>