gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:36 25 4
gpt4 key购买 nike

我读过 OnKeyListener 用于检测硬键,OnEditorActionListener 用于检测软键。然而,网上有很多例子,因此似乎它们在任何情况下都可以正常工作。

我都试过了,OnKeyListener 只能在真实设备上运行,而 OnEditorActionListener 可以在真实设备和模拟器上运行也是。

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

最佳答案

只是文档中的一小部分复制粘贴:

 /**
* 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.

同样,

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

关于android - OnKeyListener 和 OnEditorActionListener 之间的确切区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41736332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com