最近有幸做了一次输入法的开发,在此处共享一下,今天先写一部分
输入法其实就是一个服务,基本上输入法都通过继承自谷歌提供InputMethodService类来实现输入法。然后要重写几个关键的回调函数就可以了。这几个函数包括:
(1) onCreateInputView()
我们先来看看API说明:
Create and return the viewhierarchy used for the input area (such as a soft keyboard). This will be calledonce, when the input area is first displayed. You can return null to have noinput area; the default implementation returns null.
大意就是说用于创建软键盘视图,输入法启动的时候之后只调用一次。默认为空。
(2) onStartInput(EditorInfoeditorInfo, boolean restarting)
我们先来看API说明:
Called to inform the input method that text input has started inan editor. You should use this callback to initialize the state of your inputto match the state of the editor given to it.
用于通知输入法,一个文本输入已经开始。在此处根据编辑框属性初始化你的输入法状态
(3) onStartInputView(EditorInfoeditorInfo, boolean restarting)
我们先来看API说明:
Calledwhen the input view is being shown and input has started on a new editor. Thiswill always be called after onStartInput(EditorInfo, boolean),allowing you to do your general setup there and just view-specific setup here.You are guaranteed that onCreateInputView()will have been called some time before this function is called.
当启动一个输入的时候会回调这个函数,主要用于自定义一些输入法视图。
(4) onFinishInputView(booleanfinishingInput)
Calledwhen the input view is being hidden from the user. This will be called eitherprior to hiding the window, or prior to switching to another target forediting.
当输入法软键盘被隐藏的时候,(比如返回键)会调用。它会在应用的窗口消失或者切换编辑区域的之前调用,我们可以在此处做一些视图的隐藏工作。
(5) onFinishInput()
这个回调函数是在彻底结束当前输入状态的时候会调用,比如当前窗体隐藏或者切换编辑区域,但是如果是返回按键隐藏输入法则不会调用这个函数。