资料
输入法(InputMethodService)专栏-生命周期解读
生命周期图
资料的方法抄写☺
- onCreate()
输入法创建过程时首先调用该方法.由于常驻在进程当中,在没有onDestroy之前只会调用该方法一次 - onInitializeInterface()
紧随着onCreate()方法后调用,用于界面初始化以及用于service运行过程中配置信息发生改变的情况(横竖屏转换等,会触发onConfigurationChanged()方法) - onBindInput
用于发现客户端的变化,当新的客户端绑定到输入法时,该方法会被调用,在输入法第一次启动时,会马上调用onStartInput方法获取编辑框数据,否则,先调用onFinishInput方法,然后调用onStartInput方法 - onStartInput
走完onBindInput(),会调用该方法.用于处理客户端发起的输入会话,输入法可以获取到相对应的编辑框的信息,用于决定展示什么类型的键盘.在输入法周期中会频繁的调用. - onCreateInputView()
在该生命周期中主要用于初始化跟input area区域相关的类和变量 - onCreateCandidateView()
在该生命周期中主要用于初始化跟candidate areate区域相关的类和变量 - onCreateExtractTextView
在输入法全屏模式下会调用,用于创建并返回用于显示(extracted text)文本信息的区域视图,返回的视图必须包含ExtractEditText,且ID值为inputExtractEditText,默认情况下横屏模式时,输入法为全屏效果 - onConfigureWindow()
通常在窗口发生改变时候调用,比如在获取到输入视图时候调用和失去输入视图时. - onStartInputView()
输入视图正在显示并且编辑框输入已经获取焦点时回调该方法用于创建并返回(input area)输入区域的层次视图,该方法只被调用一次(输入区域第一次显示时),该方法可以返回null,此时输入法不存在输入区域,InputMethodSerivce的默认方法实现返回值为空,想要改变已经创建的输入区域视图,我们可以调用setInputView(View)方法,想要控制何时输入输入视图,我们可以实现onEvaluateInputViewShow()方法,该方法用来判断输入区域是否应该显示,在updateInputViewShow方法中会调用onEvaluateInputViewShown方法来判断是否显示输入区域. - onStartInputView()
输入视图正在显示并且编辑框输入已经获取焦点时回调该方法,onStartInputView方法总会在onStartInput,onConfigureWindow()方法之后被调用.一般情况下普通的设置可以在onStartInput方法中进行,在onStartInputView方法中进行视图相关的设置,开发者应该保证onCreateInputView方法在该方法被调用之前调用. - onStartCandidateView()
候选视图正在显示时回调该方法,必须确保候选区域的资源已经初始化过了.一般情况下普通的设置可以在onStartInput方法中进行,在onStartCandiatesView方法中进行视图相关的设置,开发者应该保证onCreateCandidatesView方法在该方法被调用. - onWindowShown()
在onStartInputView方法之后调用,调用该方法时,表示整个输入法是可见的 - hideWindow() 当输入法window失去焦点时调用该方法
- onWindwHidden()
当视图由可见转换为不可见时,调用该方法.一般跟onWindowShown配合使用. - onFinishCandidatesView()
当候选词视图即将隐藏或切换到另外的编辑框时调用该方法,finishingInput为true.onFinishInput方法被调用 - onFinishInputView()
当候选词视图即将被隐藏或者切换到另外的编辑框时候调用该方法,finishingInput为true,onFinishInput方法会接着被调用. - onFinishInput()
往往在onFinishInputView之后调用,后续可能会接着调用onStartInput方法,或者处于闲置状态,在使用输入法过程中会频繁的调用. - onUnbindInput()
与绑定的客户端失去联系时,会调用该方法 - onDestroy()
输入法服务结束时调用.只调用一次,再次方法中做好资源释放的工作.
英语方法注释
- onCreate()
Called by the system when the service is first created. Do not call this method directly. - onInitializeInterface()
This is a hook that subclasses can use to perform initialization of their interface. It is called for you prior to any of your UI objects being created, both after the service is first created and after a configuration change happens. - void onBindInput()
Called when a new client has bound to the input method. This may be followed by a series of {@link #onStartInput(EditorInfo, boolean)} and {@link #onFinishInput()} calls as the user navigates through its UI. Upon this call you know that {@link #getCurrentInputBinding} and {@link #getCurrentInputConnection} return valid objects. - void onStartInput(EditorInfo attribute, boolean restarting)
Called to inform the input method that text input has started in an editor. You should use this callback to initialize the state of your input to match the state of the editor given to it.
@param attribute The attributes of the editor that input is starting in.
@param restarting Set to true if input is restarting in the same editor such as because the application has changed the text in the editor. Otherwise will be false, indicating this is a new session with the editor. - onCreateInputView()
Create and return the view hierarchy used for the input area (such as a soft keyboard). This will be called once, when the input area is first displayed. You can return null to have no input area; the default implementation returns null.
To control when the input view is displayed, implement {@link #onEvaluateInputViewShown()}.
To change the input view after the first one is created by this function, use {@link #setInputView(View)}. - View onCreateCandidatesView()
Create and return the view hierarchy used to show candidates. This will be called once, when the candidates are first displayed. You can return null to have no candidates view; the default implementation returns null.
To control when the candidates view is displayed, use {@link #setCandidatesViewShown(boolean)}.
To change the candidates view after the first one is created by this function, use {@link #setCandidatesView(View)}. - View onCreateExtractTextView()
Called by the framework to create the layout for showing extacted text. Only called when in fullscreen mode. The returned view hierarchy must have an {@link ExtractEditText} whose ID is {@link android.R.id#inputExtractEditText}. - void onConfigurationChanged(Configuration newConfig)
Take care of handling configuration changes. Subclasses of InputMethodService generally don’t need to deal directly with this on their own; the standard implementation here takes care of regenerating the input method UI as a result of the configuration change, so you can rely on your {@link #onCreateInputView} and other methods being called as appropriate due to a configuration change.
When a configuration change does happen, {@link #onInitializeInterface()} is guaranteed to be called the next time prior to any of the other input or UI creation callbacks. The following will be called immediately depending if appropriate for current state: {@link #onStartInput} if input is active, and {@link #onCreateInputView} and {@link #onStartInputView} and related appropriate functions if the UI is displayed. - void onStartInputView(EditorInfo info, boolean restarting)
Called when the input view is being shown and input has started on a new editor. This will always be called after {@link #onStartInput}, allowing you to do your general setup there and just view-specific setup here. You are guaranteed that {@link #onCreateInputView()} will have been called some time before this function is called.
@param info Description of the type of text being edited.
@param restarting Set to true if we are restarting input on the same text field as before. - **void onStartCandidatesView(EditorInfo info, boolean restarting) **
Called when only the candidates view has been shown for showing processing as the user enters text through a hard keyboard. This will always be called after {@link #onStartInput}, allowing you to do your general setup there and just view-specific setup here. You are guaranteed that {@link #onCreateCandidatesView()} will have been called some time before this function is called.
Note that this will not be called when the input method is running in full editing mode, and thus receiving {@link #onStartInputView} to initiate that operation. This is only for the case when candidates are being shown while the input method editor is hidden but wants to show its candidates UI as text is entered through some other mechanism.
@param info Description of the type of text being edited.
@param restarting Set to true if we are restarting input on the same text field as before. - onWindowShown()
Called immediately before the input method window is shown to the user. You could override this to prepare for the window to be shown (update view structure etc). - hideWindow()
无 - onWindowHidden()
Called when the input method window has been hidden from the user, after previously being visible. - void onFinishCandidatesView(boolean finishingInput)
Called when the candidates view is being hidden from the user. This will be called either prior to hiding the window, or prior to switching to another target for editing.
The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like.
@param finishingInput If true, {@link #onFinishInput} will be called immediately after. - void onFinishInputView(boolean finishingInput)
Called when the input view is being hidden from the user. This will be called either prior to hiding the window, or prior to switching to another target for editing.
The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like.
@param finishingInput If true, {@link #onFinishInput} will be called immediately after. - void onFinishInput()
Called to inform the input method that text input has finished in the last editor. At this point there may be a call to {@link #onStartInput(EditorInfo, boolean)} to perform input in a new editor, or the input method may be left idle. This method is not called when input restarts in the same editor.
The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like. - onUnbindInput()
Called when the previous bound client is no longer associated with the input method. After returning {@link #getCurrentInputBinding} and {@link #getCurrentInputConnection} will no longer return valid objects. - onDestroy()
Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.