我正在制作一个我想包含在应用程序中的自定义键盘。我已经知道how to make a system keyboard .我不想这样做,因为它需要用户安装。
每当用户按下键盘上的一个键时,它应该将键文本发送到当前具有焦点的任何 EditText
(如果有)。
An editor needs to interact with the IME, receiving commands through this
InputConnection
interface, and sending commands throughInputMethodManager
.
如下图所示(其中 View
是 EditText
)。
这听起来像是我应该使用输入连接来与 EditText
通信。所以我的问题是,我的自定义键盘 View 如何获得对当前聚焦的 EditText
的输入连接的引用。或者它如何启动该连接?
相关
最佳答案
正如@pskink 在评论中提到的,您可以使用
InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
获取对 EditText
的输入连接的引用。
当 EditText
通过添加监听器获得焦点时,它可以传递给自定义键盘。
// get the input connection from the currently focused edit text
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
keyboard.setInputConnection(ic); // custom keyboard method
}
}
});
关于android - 如何获取(或制作)对 Android EditText 的 InputConnection 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945918/